diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5df83499d5dde29b205ee17fba81a63c9a643235..00996cb7ed5cc573c42b69be6db369c3654d6d1a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -20,8 +20,10 @@ set(PADDLE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
include(system)
project(paddle CXX C Go)
-message(STATUS "CXX compiler: " ${CMAKE_CXX_COMPILER} ", version: " ${CMAKE_CXX_COMPILER_VERSION})
-message(STATUS "C compiler: " ${CMAKE_C_COMPILER} ", version: " ${CMAKE_C_COMPILER_VERSION})
+message(STATUS "CXX compiler: ${CMAKE_CXX_COMPILER}, version: "
+ "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
+message(STATUS "C compiler: ${CMAKE_C_COMPILER}, version: "
+ "${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}")
find_package(Sphinx)
if(NOT CMAKE_CROSSCOMPILING)
diff --git a/cmake/external/eigen.cmake b/cmake/external/eigen.cmake
index 96fc886a342cae38d5b804266d3af7bc909a4da2..c4712f19eb80b34ffbf713d2b13fc0c775312af1 100644
--- a/cmake/external/eigen.cmake
+++ b/cmake/external/eigen.cmake
@@ -19,7 +19,7 @@ ExternalProject_Add(
if (${CMAKE_VERSION} VERSION_LESS "3.3.0")
set(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/eigen3_dummy.c)
- file(WRITE ${dummyfile} "const char * dummy_eigen3 = \"${dummyfile}\";")
+ file(WRITE ${dummyfile} "const char *dummy_eigen3 = \"${dummyfile}\";")
add_library(eigen3 STATIC ${dummyfile})
else()
add_library(eigen3 INTERFACE)
diff --git a/cmake/external/openblas.cmake b/cmake/external/openblas.cmake
index 97857a686b38d935b19f510ecdcb66bcca91fe03..0e79c0cc7992060cbe3b668ec927936183389eb6 100644
--- a/cmake/external/openblas.cmake
+++ b/cmake/external/openblas.cmake
@@ -30,23 +30,21 @@ IF(NOT ${CBLAS_FOUND})
CACHE FILEPATH "openblas library." FORCE)
SET(OPENBLAS_CC "${CMAKE_C_COMPILER} -Wno-unused-but-set-variable -Wno-unused-variable")
+ SET(OPENBLAS_COMMIT "v0.2.20")
IF(CMAKE_CROSSCOMPILING)
SET(OPTIONAL_ARGS HOSTCC=${HOST_C_COMPILER})
GET_FILENAME_COMPONENT(CROSS_SUFFIX ${CMAKE_C_COMPILER} DIRECTORY)
SET(CROSS_SUFFIX ${CROSS_SUFFIX}/)
IF(ANDROID)
- # arm_soft_fp_abi branch of OpenBLAS to support softfp
- # https://github.com/xianyi/OpenBLAS/tree/arm_soft_fp_abi
- SET(OPENBLAS_COMMIT "b5c96fcfcdc82945502a2303116a64d89985daf5")
IF(ANDROID_ABI MATCHES "^armeabi(-v7a)?$")
+ # use softfp
SET(OPTIONAL_ARGS ${OPTIONAL_ARGS} TARGET=ARMV7 ARM_SOFTFP_ABI=1 USE_THREAD=0)
ELSEIF(ANDROID_ABI STREQUAL "arm64-v8a")
SET(OPTIONAL_ARGS ${OPTIONAL_ARGS} TARGET=ARMV8 BINARY=64 USE_THREAD=0)
ENDIF()
ELSEIF(IOS)
IF(CMAKE_OSX_ARCHITECTURES MATCHES "arm64")
- SET(OPENBLAS_COMMIT "b5c96fcfcdc82945502a2303116a64d89985daf5")
SET(OPENBLAS_CC "${OPENBLAS_CC} ${CMAKE_C_FLAGS} -isysroot ${CMAKE_OSX_SYSROOT}")
SET(OPENBLAS_CC "${OPENBLAS_CC} -arch arm64")
SET(OPTIONAL_ARGS ${OPTIONAL_ARGS} TARGET=ARMV8 BINARY=64 USE_THREAD=0 CROSS_SUFFIX=${CROSS_SUFFIX})
@@ -56,14 +54,12 @@ IF(NOT ${CBLAS_FOUND})
ENDIF()
ELSEIF(RPI)
# use hardfp
- SET(OPENBLAS_COMMIT "v0.2.20")
SET(OPTIONAL_ARGS ${OPTIONAL_ARGS} TARGET=ARMV7 USE_THREAD=0)
ENDIF()
ELSE()
IF(APPLE)
SET(OPENBLAS_CC "${CMAKE_C_COMPILER} -isysroot ${CMAKE_OSX_SYSROOT}")
ENDIF()
- SET(OPENBLAS_COMMIT "v0.2.20")
SET(OPTIONAL_ARGS "")
IF(CMAKE_SYSTEM_PROCESSOR MATCHES "^x86(_64)?$")
SET(OPTIONAL_ARGS DYNAMIC_ARCH=1 NUM_THREADS=64)
@@ -113,7 +109,7 @@ INCLUDE_DIRECTORIES(${CBLAS_INC_DIR})
# FIXME(gangliao): generate cblas target to track all high performance
# linear algebra libraries for cc_library(xxx SRCS xxx.c DEPS cblas)
SET(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/cblas_dummy.c)
-FILE(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";")
+FILE(WRITE ${dummyfile} "const char *dummy_cblas = \"${dummyfile}\";")
ADD_LIBRARY(cblas STATIC ${dummyfile})
TARGET_LINK_LIBRARIES(cblas ${CBLAS_LIBRARIES})
diff --git a/cmake/generic.cmake b/cmake/generic.cmake
index 66c8e3ad7ef7c80c1f388c25983425a0db5c0220..585db019d521b1699baadfae31ef95b5059c71b4 100644
--- a/cmake/generic.cmake
+++ b/cmake/generic.cmake
@@ -120,7 +120,7 @@ function(merge_static_libs TARGET_NAME)
DEPENDS ${libs})
# Generate dummy staic lib
- file(WRITE ${target_SRCS} "const char *dummy = \"${target_SRCS}\";")
+ file(WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \"${target_SRCS}\";")
add_library(${TARGET_NAME} STATIC ${target_SRCS})
target_link_libraries(${TARGET_NAME} ${libs_deps})
@@ -160,7 +160,7 @@ function(merge_static_libs TARGET_NAME)
DEPENDS ${libs} ${target_OBJS})
# Generate dummy staic lib
- file(WRITE ${target_SRCS} "const char *dummy = \"${target_SRCS}\";")
+ file(WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \"${target_SRCS}\";")
add_library(${TARGET_NAME} STATIC ${target_SRCS})
target_link_libraries(${TARGET_NAME} ${libs_deps})
@@ -324,7 +324,7 @@ function(go_library TARGET_NAME)
)
# Add dummy code to support `make target_name` under Terminal Command
- file(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";")
+ file(WRITE ${dummyfile} "const char *dummy_${TARGET_NAME} = \"${dummyfile}\";")
if (go_library_SHARED OR go_library_shared)
add_library(${TARGET_NAME} SHARED ${dummyfile})
else()
diff --git a/doc/api/v2/fluid/layers.rst b/doc/api/v2/fluid/layers.rst
index 004ee2d8c85ce7661886179570e693d7d61bc6d8..a7c8670f66cc7f319e41155211ead2d89126117f 100644
--- a/doc/api/v2/fluid/layers.rst
+++ b/doc/api/v2/fluid/layers.rst
@@ -307,6 +307,12 @@ sequence_expand
:noindex:
+gru_unit
+--------
+.. autofunction:: paddle.v2.fluid.layers.gru_unit
+ :noindex:
+
+
lstm_unit
---------
.. autofunction:: paddle.v2.fluid.layers.lstm_unit
diff --git a/doc/design/concurrent_programming.md b/doc/design/concurrent_programming.md
new file mode 100644
index 0000000000000000000000000000000000000000..afc65e831d58ff427663806e56294292ccbef85b
--- /dev/null
+++ b/doc/design/concurrent_programming.md
@@ -0,0 +1,163 @@
+# Design Doc: Concurrent Programming with Fluid
+
+With PaddlePaddle Fluid, users describe a program other than a model. The program is a [`ProgramDesc`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/framework.proto) protobuf message. TensorFlow/MxNet/Caffe2 applications generate protobuf messages too, but their protobuf messages represent the model, a graph of operators, but not the program that trains/uses the model.
+
+Many know that when we program TensorFlow, we can specify the device on which each operator runs. This allows us to create a concurrent/parallel AI application. An interesting questions is **how does a `ProgramDesc` represents a concurrent program?**
+
+The answer relies on the fact that a `ProgramDesc` is similar to an abstract syntax tree (AST) that describes a program. So users just program a concurrent program that they do with any concurrent programming language, e.g., [Go](https://golang.org).
+
+## An Analogy
+
+The following table compares concepts in Fluid and Go
+
+| Go | Fluid |
+|----|-------|
+|user-defined functions | [layers](https://github.com/PaddlePaddle/Paddle/tree/develop/python/paddle/v2/fluid) |
+| control-flow and built-in functions | [intrinsics/operators](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/operators) |
+| goroutines, channels | [class ThreadPool](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/framework/thread_pool.h) |
+| runtime | [class Executor](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/executor.h) |
+
+## An Example Concurrent Program
+
+To review all above concepts in an example, let us take a simple program and writes its distributed version.
+
+Suppose that we want to parallelize a naive Fluid program (written in Go and calling Fluid's Go binding) that multiplies two tensors.
+
+```go
+import "fluid"
+
+func paddlepaddle() {
+ X = fluid.read(...)
+ W = fluid.Tensor(...)
+ Y = fluid.mult(X, W)
+}
+```
+
+Please be aware that the Fluid's Go binding provides the default `main` function, which calls the `paddlepaddle` function, which, in this case, is defined in above program and creates the following `ProgramDesc` message.
+
+```protobuf
+message ProgramDesc {
+ block[0] = Block {
+ vars = [X, W, Y],
+ ops = [
+ read(output = X)
+ assign(input = ..., output = W)
+ mult(input = {X, W}, output = Y)
+ ],
+ }
+}
+```
+
+Then, the default `main` function calls `fluid.run()`, which creates an instance of the [`class Executor`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/executor.h) and calls `Executor.Run(block[0])`, where `block[0]` is the first and only block defined in above `ProgramDesc` message.
+
+The default `main` function is defined as follows:
+
+```go
+func main() {
+ paddlepaddle()
+ fluid.run()
+}
+```
+
+## The Concurrent Version
+
+By parallelizing the above program, we could support very big tensor X by splitting into small pieces {x_1, x_2, ...} and sent each piece to worker process/node for parallel multiplication.
+
+In this case, we can write a transpiler that takes a `ProgramDesc` message that represents the above example program and outputs two `ProgramDesc` messages, one for running on the master process/node, and the other one for worker processes/nodes.
+
+### The Master Program
+
+The master program could look like the following:
+
+```protobuf
+message ProgramDesc {
+ block[0] = Block {
+ vars = [X, L, Y],
+ ops = [
+ read(output = X)
+ kube_get_workers_addrs(output = L)
+ Y = tensor_array(len(L))
+ parallel_for(input = X, output = Y,
+ attrs = {L, block_id(1)}) # referring to block 1
+ ]
+ }
+
+ block[1] = Block {
+ parent = 0,
+ vars = [x, y, index],
+ ops = [
+ slice(input = [X, index], output = x) # index is initialized by parallel_for
+ send(input = x, attrs = L[index])
+ recv(outputs = y, attrs = L[index])
+ assign(input = y, output = Y[index])
+ ]
+ }
+}
+```
+
+The equivalent Fluid program (calling the Go binding) is:
+
+```go
+func main() { //// block 0
+ X = fluid.read(...)
+ L = fluid.k8s.get_worker_addrs()
+ Y = fluid.tensor_array(len(L))
+ fluid.parallel_for(X, L,
+ func(index int) { //// block 1
+ x = X[index]
+ fluid.send(L[index], x)
+ y = fluid.recv(L[index])
+ Y[index] = y
+ })
+}
+```
+
+An explanation of the above program:
+
+- `fluid.k8s` is a package that provides access to Kubernetes API.
+- `fluid.k8s.get_worker_addrs` returns the list of IP and ports of all pods of the current job except for the current one (the master pod).
+- `fluid.tensor_array` creates a [tensor array](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/lod_tensor_array.h). `fluid.parallel_for` creates a `ParallelFor` intrinsic, which, when executed,
+
+ 1. creates `len(L)` scopes, each for the concurrent running of the sub-block (block 1 in this case), and initializes a variable named "index" in the scope to an integer value in the range `[0, len(L)-1]`, and
+ 2. creates `len(L)` threads by calling into the `ThreadPool` singleton, each thread
+ 1. creates an Executor instance, and
+ 2. calls `Executor.Run(block)`, where `block` is block 1 as explained above.
+1. Please be aware that block 1 is a sub-block of block 0, so ops in block 1 could refer to variables defined in block 0.
+
+### The Worker Program
+
+The worker program looks like
+
+```go
+func main() {
+ W = Tensor(...)
+ x = fluid.listen_and_do(
+ fluid.k8s.self_addr(),
+ func(input Tensor) {
+ output = fluid.mult(input, W)
+ })
+}
+```
+
+where
+
+- `fluid.listen_and_do` creates a `ListenAndDo` intrinsic, which, when executed,
+ 1. listens on the current pod's IP address, as returned by `fliud.k8s.self_addr()`,
+ 2. once a connection is established,
+ 1. creates a scope of two parameters, "input" and "output",
+ 2. reads a [Fluid variable](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/variable.h) and saves it into "input",
+ 3. creates an Executor instance and calls `Executor.Run(block)`, where the block is generated by running the lambda specified as the second parameter of `fluid.listen_and_do`.
+
+## Summarization
+
+From the above example, we see that:
+
+1. Fluid enables the imperative programming paradigm by:
+ 1. letting users describe a program, but not a model (a sequence of layers, or a graph of operators), and
+ 2. call the `fluid.run` function that runs the program implicitly.
+1. The program is described as a `ProgramDesc` protobuf message.
+2. Function `Executor.Run` takes a block, instead of a `ProgramDesc`, as its parameter.
+3. `fluid.run` calls `Executor.Run` to run the first block in the `ProgramDesc` message.
+4. `Executor.Run`'s implementation is extremely simple -- it doesn't plan the execution nor create threads; instead, it runs on the current thread and execute intrinsics/operators' `Run` method sequentially as they appear in the `Block.ops` array.
+5. Intrinsics/operators' `Run` method might create threads. For example, the `ListenAndDo` operator creates a thread to handle each incoming request.
+6. Threads are not necessarily OS thread; instead, they could be [green threads](https://en.wikipedia.org/wiki/Green_threads) managed by ThreadPool. Multiple green threads might run on the same OS thread. An example green threads is Go's [goroutines](https://tour.golang.org/concurrency/1).
diff --git a/doc/design/images/control_flow_graph.png b/doc/design/images/control_flow_graph.png
new file mode 100644
index 0000000000000000000000000000000000000000..3579998e58d07abc50bd3332128d4733a391cb3b
Binary files /dev/null and b/doc/design/images/control_flow_graph.png differ
diff --git a/doc/design/images/dataflow_equations.png b/doc/design/images/dataflow_equations.png
new file mode 100644
index 0000000000000000000000000000000000000000..c10f7f69f4007952e5b0394edaa04efa1cfbb658
Binary files /dev/null and b/doc/design/images/dataflow_equations.png differ
diff --git a/doc/design/images/deep_learning.png b/doc/design/images/deep_learning.png
new file mode 100644
index 0000000000000000000000000000000000000000..026becc4d94e01e407dacb2a5314a0e5723334ff
Binary files /dev/null and b/doc/design/images/deep_learning.png differ
diff --git a/doc/design/memory_optimization.md b/doc/design/memory_optimization.md
new file mode 100644
index 0000000000000000000000000000000000000000..00f514711a46bfd5af3bae51e0d9225ecc4c8998
--- /dev/null
+++ b/doc/design/memory_optimization.md
@@ -0,0 +1,217 @@
+# Memory Optimization
+
+
+## Problem
+
+In a lecture from Andrew Ng, he attributes the recent sucess of AI due to a combination of these:
+
+- availability of Big Data
+- supercomputing power to process this Big Data over very large neural networks
+- modern algorithms
+
+Following graph shows the details:
+
+![](images/deep_learning.png)
+
+Larger model usually brings better performance. However, GPU memory is certain limited. For example, the memory size of a GTX TITAN X is only 12GB. To train complex and large model, we have to take care of memory using. Besides, memory optimization is also necessary in both online/mobile inference.
+
+## Solution
+
+### Basic Strategy
+
+There are some basic strategies to make memory optimization, including in-place operation and memory sharing.
+
+#### In-place Operation
+In a relu activation operator:
+
+$y = \max(x, 0)$
+
+If the variable x is not used in any other operator, we can make an in-place operation. In other words, the memory block of variable y and variable x are the same. In-place operation will save 50% memory occupancy immediately.
+
+#### Memory Sharing
+
+Not all operators support in-place operations. Memory sharing is a more general strategy.
+
+Following is an example:
+
+```
+a = op1(b, c);
+d = op2(a)
+e = op3(d, f)
+```
+
+In this case, variable a is no longer used, and op2 does not support in-place operation. After op2 finished, we can put the memory of variable a to a memory pool. Then, variable e can share the memory of variable a from the pool.
+
+
+### Live Variable Analysis
+
+It's not enough to only have some basic strategies. The prerequisite of memory optimization is to know if a variable is still "live" after an operation.
+
+In our design, the neural network topology is defined as a program. Luckily, [live variable analysis](https://en.wikipedia.org/wiki/Live_variable_analysis) is a classic problem in compilers which can be used in many stages, such as register allocation.
+
+In compilers, the front end of the compilers translates programs into an intermediate language with an unbounded number of temporaries. This program must run on a machine with a bounded number of registers. Two temporaries a and b can fit into the same register, if a and b are never "in use" at the same time. Thus, many temporaries can fit in few registers; if they don't all fit, the excess temporaries can be kept in memory.
+
+Therefore, the compiler needs to analyze the intermediate-representation program to determine which temporaries are in use at the same time. We say a variable is "live" if it holds a value that may be needed in the future, so this analysis is called liveness analysis.
+
+We can leran these techniques from compilers. There are mainly two stages to make live variable analysis:
+
+- construct a control flow graph
+- solve the dataflow equations
+
+
+#### Control Flow Graph
+To preform analyses on a program, it is often useful to make a control flow graph. A [control flow graph](https://en.wikipedia.org/wiki/Control_flow_graph) (CFG) in computer science is a representation, using graph notation, of all paths that might be traversed through a program during its execution. Each statement in the program is a node in the flow graph; if statemment x can be followed by statement y, there is an egde from x to y.
+
+Following is the flow graph for a simple loop.
+
+![](images/control_flow_graph.png)
+
+#### Dataflow Analysis
+
+liveness of variable "flows" around the edges of the control flow graph; determining the live range of each variable is an example of a dataflow problem. [Dataflow analysis](https://en.wikipedia.org/wiki/Data-flow_analysis) is a technique for gathering information about the possible set of values calculated at various points in a computer program.
+
+A simple way to perform data-flow analysis of programs is to set up dataflow equations for each node of the control flow graph and solve them by repeatedly calculating the output from the input locally at each node until the whole system stabilizes.
+
+- Flow Graph Terminology
+
+A flow graph node has out-edges that lead to sucessor nodes, and in-edges that come from presucessor nodes. The set *pred[n]* is all the predecessors of node n, and *succ[n]* is the set of sucessors.
+In former control flow graph, the out-edges of node 5 are 5 --> 6 and 5 --> 2, and *succ[5]* = {2, 6}. The in-edges of 2 are 5 --> 2 and 1 --> 2, and *pred[2]* = {1, 5}.
+
+- Uses and Defs
+
+An assignmemt to a variable or temporary defines that variable. An occurence of a variable on the right-hand side of an assginment(or in other expressions) uses the variable. We can speak the *def* of a variable as the set of graph nodes that define it; or the *def* of a graph node as the set of variables that it defines; and the similarly for the *use* of a variable or graph node. In former control flow graph, *def(3)* = {c}, *use(3)* = {b, c}.
+
+- Liveness
+
+A variable is *live* on an edge if there is a directed path from that edge to a *use* of the variable that does not go through any *def*. A variable is *live-in* at a node if it is live on any of the in-edges of that node; it is *live-out* at a node if it is live on any of the out-edges of the node.
+
+
+The calcution of liveness can be solved by iteration until a fixed pointer is reached. Following is the recursive formula:
+
+![](images/dataflow_equations.png)
+
+### Memory optimization transpiler
+
+At last, we take basic strategy and liveness analysis techniques learning from compilers to implement our memory optimization transpiler.
+
+#### add in-place attribute
+
+In-place is a built-in attribute of an operator. Since we treat in-place and other operators differently, we have to add an in-place attribute for every operator.
+
+
+#### contruct control flow graph
+
+Following is the ProgramDesc protobuf of [machine translation](https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/fluid/tests/book/test_machine_translation.py) example.
+
+- Block0:
+
+```
+lookup_table
+mul
+...
+while(sub-block idx 1)
+...
+array_to_lod_tensor
+cross_entropy
+...
+while_grad(sub-block idx 2)
+read_from_array
+array_to_lod_tensor
+...
+```
+
+- Block1
+
+```
+read_from_array
+read_from_array
+...
+write_to_array
+increment
+write_to_array
+less_than
+```
+
+- Block2
+
+```
+read_from_array
+increment
+...
+write_to_array
+write_to_array
+```
+
+We can transfer all the operators and variables in ProgramDesc to build a control flow graph.
+
+```python
+class ControlFlowGraph(object):
+ def __init__(self, Program):
+ self._sucessors = defaultdict(set)
+ self._presucessors = defaultdict(set)
+ self._uses = defaultdict(set)
+ self._defs = defaultdict(set)
+ self._live_in = defaultdict(set)
+ self._live_out = defaultdict(set)
+ self._program = Program
+
+ def build(self):
+ pass
+
+ def dataflow_analysis(self):
+ pass
+
+ def memory_optimization(self):
+ pass
+
+ def get_program(self):
+ return self._program
+```
+
+#### make dataflow analysis
+
+We follow guide from compilers and try to solve the dataflow equation to get liveness of every variable. If the live-in of an operator node is different from the live-out, then we can make memory sharing.
+
+For example:
+
+```
+a = op1(b, c);
+d = op2(a)
+e = op3(d, f)
+```
+
+The dataflow analysis result is:
+
+```
+live_in(op1) = {b, c, f}
+live_out(op1) = {a, f}
+
+live_in(op2) = {a, f}
+live_out(op2) = {d, f}
+
+live_in(op3) = {d, f}
+live_out(op3) = {}
+```
+
+After op1, we can process variable b and variable c; After op2, we can process variable a. After op3, we can process variable d and variable f.
+
+#### memory sharing policy
+
+A memory pool will be mantained in the stage of memory optimization. Each operator node will be scanned to determine memory optimization is done or not. If an operator satifies the requirement, following policy will be taken to handle input/output variables.
+
+```
+if op.support_inplace():
+ i --> pool
+ pool --> o
+else:
+ pool --> o
+ i --> pool
+```
+
+
+
+## Reference
+
+- [Lecture Notes From Artificial Intelligence Is The New Electricity By Andrew Ng](https://manavsehgal.com/lecture-notes-from-artificial-intelligence-is-the-new-electricity-by-andrew-ng-4712dcbf26e5)
+- Modern compiler implementation in ML, by Andrew W. Appel
+- [Optimizing Memory Consumption in Deep learning](https://mxnet.incubator.apache.org/architecture/note_memory.html)
diff --git a/doc/design/support_new_device.md b/doc/design/support_new_device.md
index f54b2b3694cc2a8f1d892792fd4d39a0484dc750..4c5f10e2ecb9ec09b78926ca27552741d02d7cc9 100644
--- a/doc/design/support_new_device.md
+++ b/doc/design/support_new_device.md
@@ -48,8 +48,8 @@ Fluid uses class [DeviceContext](https://github.com/PaddlePaddle/Paddle/blob/dev
```
- /-> CPUDeviceContext --> MKLDeviceContext
-DeviceContext ----> CUDADeviceContext --> CUDNNDeviceContext
+ /-> CPUDeviceContext
+DeviceContext ----> CUDADeviceContext
\-> FPGADeviceContext
```
@@ -79,16 +79,6 @@ private:
};
```
-- CUDNNDeviceContext
-
-```
-class CUDNNDeviceContext : public CUDADeviceContext {
- private:
- cudnnHandle_t cudnn_handle_;
-};
-```
-
-
### Memory and Tensor
diff --git a/doc/mobile/cross_compiling_for_android_cn.md b/doc/mobile/cross_compiling_for_android_cn.md
index 424d7718c64438496cf0895397babd5408e1ca02..ae24ced770492743065e37654b494caf6b4c5bc0 100644
--- a/doc/mobile/cross_compiling_for_android_cn.md
+++ b/doc/mobile/cross_compiling_for_android_cn.md
@@ -1,8 +1,9 @@
# Android平台编译指南
用户可通过如下两种方式,交叉编译Android平台上适用的PaddlePaddle库:
-- 基于Docker容器的编译方式
-- 基于Linux交叉编译环境的编译方式
+
+- [基于Docker容器的编译方式](#基于docker容器的编译方式)
+- [基于Linux交叉编译环境的编译方式](#基于linux交叉编译环境的编译方式)
## 基于Docker容器的编译方式
Docker能在所有主要操作系统(包括Linux,Mac OS X和Windows)上运行,因此,使用基于Docker容器的编译方式,用户可在自己熟悉的开发平台上编译Android平台上适用的PaddlePaddle库。
@@ -16,6 +17,12 @@ $ cd Paddle
$ docker build -t username/paddle-android:dev . -f Dockerfile.android
```
+用户也可以使用PaddlePaddle提供的官方开发镜像:
+
+```bash
+$ docker pull paddlepaddle/paddle:latest-dev-android
+```
+
### 编译PaddlePaddle C-API库
构建好开发镜像后,即可使用开发镜像来编译Android版PaddlePaddle C-API库。
Android的Docker开发镜像向用户提供两个可配置的参数:
@@ -41,23 +48,25 @@ Android的Docker开发镜像向用户提供两个可配置的参数:
ANDROID_API |
- >= 21 |
+ >= 16 |
21 |
- 编译`armeabi-v7a`,`Android API 21`的PaddlePaddle库
+
```bash
$ docker run -it --rm -v $PWD:/paddle -e "ANDROID_ABI=armeabi-v7a" -e "ANDROID_API=21" username/paddle-android:dev
```
- 编译`arm64-v8a`,`Android API 21`的PaddlePaddle库
+
```bash
$ docker run -it --rm -v $PWD:/paddle -e "ANDROID_ABI=arm64-v8a" -e "ANDROID_API=21" username/paddle-android:dev
```
-执行上述`docker run`命令时,容器默认执行[paddle/scripts/docker/build_android.sh](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/scripts/docker/build_android.sh)脚本。该脚本中记录了交叉编译Android版PaddlePaddle库常用的CMake配置,并且会根据`ANDROID_ABI`和`ANDROID_API`自动构建独立工具链、进行编译和安装。由于arm64架构要求Android API不小于21。因此当`ANDROID_ABI=arm64-v8a`,`ANDROID_API<21`时,Docker容器中将默认使用`Android API 21`的编译工具链。用户可以参考下文**配置交叉编译参数**章节,根据个人的需求修改定制Docker容器所执行的脚本。编译安装结束之后,PaddlePaddle的C-API库将被安装到`$PWD/install_android`目录,所依赖的第三方库同时也被安装到`$PWD/install_android/third_party`目录。
+执行上述`docker run`命令时,容器默认执行[paddle/scripts/docker/build_android.sh](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/scripts/docker/build_android.sh)脚本。该脚本中记录了交叉编译Android版PaddlePaddle库常用的CMake配置,并且会根据`ANDROID_ABI`和`ANDROID_API`自动构建独立工具链、进行编译和安装。由于arm64架构要求Android API不小于21。因此当`ANDROID_ABI=arm64-v8a`,`ANDROID_API<21`时,Docker容器中将默认使用`Android API 21`的编译工具链。用户可以参考下文[配置交叉编译参数](#配置交叉编译参数)章节,根据个人的需求修改定制Docker容器所执行的脚本。编译安装结束之后,PaddlePaddle的C-API库将被安装到`$PWD/install_android`目录,所依赖的第三方库同时也被安装到`$PWD/install_android/third_party`目录。
## 基于Linux交叉编译环境的编译方式
本文档将以Linux x86-64平台为例,介绍交叉编译Android平台上适用的PaddlePaddle库的方法和步骤。
@@ -83,6 +92,7 @@ your/path/to/android-ndk-r14b-linux-x86_64/build/tools/make-standalone-toolchain
此命令将在`your/path/to/arm_standalone_toolchain`目录生成一套独立编译工具链,面向架构为32位ARM架构,支持的最小的Android API级别为21,支持编译器`arm-linux-androideabi-gcc (GCC) 4.9`和`clang 3.8`。
- 构建`arm64-v8a`、 `Android API 21`的独立工具链:
+
```bash
your/path/to/android-ndk-r14b-linux-x86_64/build/tools/make-standalone-toolchain.sh \
--arch=arm64 --platform=android-21 --install-dir=your/path/to/arm64_standalone_toolchain
@@ -90,14 +100,12 @@ your/path/to/android-ndk-r14b-linux-x86_64/build/tools/make-standalone-toolchain
此命令将在`your/path/to/arm64_standalone_toolchain`目录生成一套独立编译工具链,面向架构为64位ARM64架构,支持的最小Android API级别为21,支持编译器`arm-linux-androideabi-gcc (GCC) 4.9`和`clang 3.8`。
-注意:**PaddlePaddle要求使用的编译工具链所支持的Android API级别不小于21**。
-
### 配置交叉编译参数
CMake系统对交叉编译提供了支持[cmake-toolchains](https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling)。为了简化cmake配置,PaddlePaddle为交叉编译提供了工具链配置文档[cmake/cross_compiling/android.cmake](https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/android.cmake),以提供一些默认的编译器和编译参数相关配置。注意,从CMake 3.7版本开始,CMake官方对Android平台的交叉编译提供了通用的支持。PaddlePaddle若检测到用户使用的CMake版本不低于3.7时,将会将用户传进来的配置参数传递CMake系统,交由CMake系统本身来处理。有关参数配置的详细说明见[cmake-toolchains](https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling)。
交叉编译Android版本的PaddlePaddle库时,有一些必须配置的参数:
-- `CMAKE_SYSTEM_NAME`,CMake编译的目标平台,必须设置为`Android`。在设置`CMAKE_SYSTEM_NAME=Android`后,PaddlePaddle的CMake系统才认为是在交叉编译Android系统的版本,并自动编译宿主机版protoc可执行文件、目标机版protobuf库、以及Android所需`arm_soft_fp_abi`分支的目标机版OpenBLAS库。此外,还会强制设置一些PaddlePaddle参数的值(`WITH_GPU=OFF`、`WITH_AVX=OFF`、`WITH_PYTHON=OFF`、`WITH_RDMA=OFF`)。
+- `CMAKE_SYSTEM_NAME`,CMake编译的目标平台,必须设置为`Android`。在设置`CMAKE_SYSTEM_NAME=Android`后,PaddlePaddle的CMake系统才认为是在交叉编译Android系统的版本,并自动编译PaddlePaddle所需的所有第三方库。此外,还会强制设置一些PaddlePaddle参数的值(`WITH_GPU=OFF`、`WITH_AVX=OFF`、`WITH_PYTHON=OFF`、`WITH_RDMA=OFF`、`WITH_MKL=OFF`、`WITH_GOLANG=OFF`)。
- `WITH_C_API`,必须设置为`ON`。在Android平台上只支持使用C-API来预测。
- `WITH_SWIG_PY`,必须设置为`OFF`。在Android平台上不支持通过swig调用来训练或者预测。
@@ -119,7 +127,7 @@ Android平台可选配置参数:
其他配置参数:
- `USE_EIGEN_FOR_BLAS`,是否使用Eigen库进行矩阵计算。可设置`ON/OFF`,默认值为`OFF`。
-- `HOST_C/CXX_COMPILER`,宿主机的C/C++编译器。在编译宿主机版protoc可执行文件和目标机版OpenBLAS库时需要用到。默认设置成环境变量`CC`的值;若环境变量`CC`没有设置,则设置成`cc`编译器。
+- `HOST_C/CXX_COMPILER`,宿主机的C/C++编译器。在编译宿主机版protoc可执行文件和目标机版OpenBLAS库时需要用到。默认设置成环境变量`CC/CXX`的值;若环境变量`CC/CXX`没有设置,则设置成`cc/c++`编译器。
常用的cmake配置如下:
@@ -147,9 +155,10 @@ cmake -DCMAKE_SYSTEM_NAME=Android \
..
```
-用户还可根据自己的需求设置其他编译参数。比如希望最小化生成的库的大小,可以设置`CMAKE_BUILD_TYPE`为`MinSizeRel`;若希望最快的执行速度,则可设置`CMAKE_BUILD_TYPE`为`Release`。亦可以通过手动设置`CMAKE_C/CXX_FLAGS_MINSIZEREL/RELEASE`来影响PaddlePaddle的编译过程。
+用户还可根据自己的需求设置其他编译参数。比如希望最小化生成的库的大小,可以设置`CMAKE_BUILD_TYPE`为`MinSizeRel`;若希望最快的执行速度,则可设置`CMAKE_BUILD_TYPE`为`Release`。亦可以通过手动设置`CMAKE_C/CXX_FLAGS`来影响PaddlePaddle的编译过程。
**性能TIPS**,为了达到最快的计算速度,在CMake参数配置上,有以下建议:
+
- 设置`CMAKE_BUILD_TYPE`为`Release`
- 使用`clang`编译工具链
- `armeabi-v7a`时,设置`USE_EIGEN_BLAS=ON`,使用Eigen进行矩阵计算;`arm64-v8a`时,设置`USE_EIGEN_FOR_BLAS=OFF`,使用OpenBLAS进行矩阵计算
diff --git a/doc/mobile/cross_compiling_for_android_en.md b/doc/mobile/cross_compiling_for_android_en.md
index 26858581fc1d77a9391520ac0dfd80fbd98f508c..0cf50181df4116beda3aa6faf836eda92edf6066 100644
--- a/doc/mobile/cross_compiling_for_android_en.md
+++ b/doc/mobile/cross_compiling_for_android_en.md
@@ -1,6 +1,9 @@
# Build PaddlePaddle for Android
-There are two approaches to build PaddlePaddle for Android: using Docker and on Linux without Docker.
+There are two approaches to build PaddlePaddle for Android:
+
+- [Cross-Compiling Using Docker](#cross-compiling-using-docker)
+- [Cross-Compiling on Linux](#cross-compiling-on-linux)
## Cross-Compiling Using Docker
@@ -16,6 +19,12 @@ $ cd Paddle
$ docker build -t paddle:dev-android . -f Dockerfile.android
```
+Users can directly use the published Docker image.
+
+```bash
+$ docker pull paddlepaddle/paddle:latest-dev-android
+```
+
### Build the Inference Library
We can run the Docker image we just created to build the inference library of PaddlePaddle for Android using the command below:
@@ -47,7 +56,7 @@ The Docker image accepts two arguments `ANDROID_ABI` and `ANDROID_API`:
ANDROID_API |
- >= 21 |
+ >= 16 |
21 |
@@ -93,15 +102,13 @@ Android NDK includes everything we need to build the [*standalone toolchain*](ht
The generated standalone toolchain will be in `your/path/to/arm64_standalone_toolchain`.
-**Please be aware that the minimum level of Android API required by PaddlePaddle is 21.**
-
### Cross-Compiling Arguments
CMake supports [choosing the toolchain](https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling). PaddlePaddle provides [`android.cmake`](https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/android.cmake), which configures the Android cross-compiling toolchain for CMake. `android.cmake` is not required for CMake >= 3.7, which support Android cross-compiling. PaddlePaddle detects the CMake version, for those newer than 3.7, it uses [the official version](https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling).
Some other CMake arguments you need to know:
-- `CMAKE_SYSTEM_NAME` must be `Android`. This tells PaddlePaddle's CMake system to cross-compile third-party dependencies. This also changes some other CMake arguments like `WITH_GPU=OFF`, `WITH_AVX=OFF`, `WITH_PYTHON=OFF`, and `WITH_RDMA=OFF`.
+- `CMAKE_SYSTEM_NAME` must be `Android`. This tells PaddlePaddle's CMake system to cross-compile third-party dependencies. This also changes some other CMake arguments like `WITH_GPU=OFF`, `WITH_AVX=OFF`, `WITH_PYTHON=OFF`, `WITH_RDMA=OFF`, `WITH_MKL=OFF` and `WITH_GOLANG=OFF`.
- `WITH_C_API` must be `ON`, to build the C-based inference library for Android.
- `WITH_SWIG_PY` must be `OFF` because the Android platform doesn't support SWIG-based API.
@@ -123,7 +130,7 @@ Some Android-specific arguments:
Other useful arguments:
- `USE_EIGEN_FOR_BLAS`: indicates if using Eigen. Could be `ON` or `OFF`, defaults to `OFF`.
-- `HOST_C/CXX_COMPILER`: specifies the host compiler, which is used to build the host-specific protoc and target-specific OpenBLAS. It defaults to the value of the environment variable `CC`, or `cc`.
+- `HOST_C/CXX_COMPILER`: specifies the host compiler, which is used to build the host-specific protoc and target-specific OpenBLAS. It defaults to the value of the environment variable `CC/C++`, or `cc/c++`.
Some frequent configurations for your reference:
@@ -158,6 +165,7 @@ There are some other arguments you might want to configure.
- `CMAKE_BUILD_TYPE-Release` optimizes the runtime performance.
Our own tip for performance optimization to use clang and Eigen or OpenBLAS:
+
- `CMAKE_BUILD_TYPE=Release`
- `ANDROID_TOOLCHAIN=clang`
- `USE_EIGEN_BLAS=ON` for `armeabi-v7a`, or `USE_EIGEN_FOR_BLAS=OFF` for `arm64-v8a`.
diff --git a/doc/mobile/cross_compiling_for_ios_en.md b/doc/mobile/cross_compiling_for_ios_en.md
index aa390cd61f3fbd75e5a3b342f3559e76da35a918..19bfe86c511c7e43b462f94c8cabba420b3007f1 100644
--- a/doc/mobile/cross_compiling_for_ios_en.md
+++ b/doc/mobile/cross_compiling_for_ios_en.md
@@ -1,4 +1,4 @@
-# PaddlePaddle Compiling Guide for iOS
+# Build PaddlePaddle for iOS
This tutorial will walk you through cross compiling the PaddlePaddle library for iOS from the source in MacOS.
@@ -98,7 +98,7 @@ You can set other compiling parameters for your own need. I.E. if you are trying
- set `CMAKE_BUILD_TYPE` with `Release`
- set `IOS_USE_VECLIB_FOR_BLAS` with `ON`
-## Compile and install
+## Build and install
After CMake, run following commands, PaddlePaddle will download the compile 3rd party dependencies, compile and install PaddlePaddle inference library.
@@ -109,7 +109,7 @@ $ make install
Please Note: if you compiled PaddlePaddle in the source directory for other platforms, do remove `third_party` and `build` directory within the source with `rm -rf` to ensure that all the 3rd party libraries dependencies and PaddlePaddle is newly compiled with current CMake configuration.
-`your/path/to/install` directory will have following directories after `compile` and `install`:
+`your/path/to/install` directory will have following directories after `make install`:
- `include`, contains all the C-API header files.
- `lib`, contains PaddlePaddle C-API static library.
diff --git a/paddle/CMakeLists.txt b/paddle/CMakeLists.txt
index 7d2becbdd772747d77890321fce6721d8d17fb30..4a98ede278fad85ff2beef3c8e7dd158912f693a 100644
--- a/paddle/CMakeLists.txt
+++ b/paddle/CMakeLists.txt
@@ -24,6 +24,7 @@ else()
add_subdirectory(framework)
add_subdirectory(operators)
add_subdirectory(pybind)
+ add_subdirectory(inference)
endif()
if(WITH_SWIG_PY)
diff --git a/paddle/framework/CMakeLists.txt b/paddle/framework/CMakeLists.txt
index b4458eb9551724021636b628c5bf8c96f6e659aa..528e45b51099d97a1f6f0dfc971b6231f928af94 100644
--- a/paddle/framework/CMakeLists.txt
+++ b/paddle/framework/CMakeLists.txt
@@ -26,7 +26,10 @@ nv_test(lod_tensor_gpu_test SRCS lod_tensor_test.cu DEPS lod_tensor)
cc_test(variable_test SRCS variable_test.cc)
-cc_library(scope SRCS scope.cc DEPS glog)
+cc_library(threadpool SRCS threadpool.cc)
+cc_test(threadpool_test SRCS threadpool_test.cc DEPS threadpool)
+
+cc_library(scope SRCS scope.cc DEPS glog threadpool)
cc_test(scope_test SRCS scope_test.cc DEPS scope)
cc_library(data_transform SRCS data_transform.cc DEPS math_function tensor framework_proto)
@@ -70,9 +73,7 @@ cc_test(var_type_inference_test SRCS var_type_inference_test.cc DEPS op_registry
cc_library(selected_rows SRCS selected_rows.cc DEPS tensor)
cc_test(selected_rows_test SRCS selected_rows_test.cc DEPS selected_rows)
-cc_library(threadpool SRCS threadpool.cc)
-cc_test(threadpool_test SRCS threadpool_test.cc DEPS threadpool)
-cc_library(init SRCS init.cc DEPS gflags device_context place stringpiece)
+cc_library(init SRCS init.cc DEPS gflags device_context place stringpiece operator)
cc_test(init_test SRCS init_test.cc DEPS init)
cc_test(op_kernel_type_test SRCS op_kernel_type_test.cc DEPS place device_context framework_proto)
diff --git a/paddle/framework/backward.cc b/paddle/framework/backward.cc
index eaf13ddcefcd8dc5a6b0438f765d8d325925aa30..85e693434af863bfc3bde29989dbbfc69678d3b7 100644
--- a/paddle/framework/backward.cc
+++ b/paddle/framework/backward.cc
@@ -427,7 +427,8 @@ std::vector> MakeBlockBackward(
VLOG(5) << "Making backward " << (*it)->Type() << " op";
std::vector> op_grads;
- if ((*it)->Type() == "recurrent" || (*it)->Type() == "while") {
+ if ((*it)->Type() == "recurrent" || (*it)->Type() == "while" ||
+ (*it)->Type() == "parallel_do") {
int step_block_idx = (*it)->GetBlockAttr("sub_block");
BlockDesc* backward_block = CreateStepBlock(program_desc, no_grad_vars,
grad_to_var, step_block_idx);
diff --git a/paddle/framework/data_transform.cc b/paddle/framework/data_transform.cc
index ac6e40a3ae8f6655eecc8279543d6afe6bbb1399..6b1780968867067fe6d1e7fc576811f0a07340b3 100644
--- a/paddle/framework/data_transform.cc
+++ b/paddle/framework/data_transform.cc
@@ -37,6 +37,28 @@ auto KernelNHWC = OpKernelType(proto::DataType::FP64, platform::CPUPlace(),
auto KernelNCHW = OpKernelType(proto::DataType::FP64, platform::CPUPlace(),
DataLayout::kNCHW, LibraryType::kPlain);
+// TODO(dzhwinter): Only for testing multiple op kernel.
+// Dummy transform function for library_type
+// should be removed.
+auto KernelPlain = OpKernelType(proto::DataType::FP32, platform::CUDAPlace(0),
+ DataLayout::kAnyLayout, LibraryType::kPlain);
+
+auto KernelCUDNN = OpKernelType(proto::DataType::FP32, platform::CUDAPlace(0),
+ DataLayout::kAnyLayout, LibraryType::kCUDNN);
+
+void DummyTrans(const platform::DeviceContext* ctx,
+ const KernelTypePair& kernel_pair, const Variable& in,
+ Variable* out) {
+ PADDLE_ENFORCE(in.IsType(), "Only Support Tensor transform!.");
+ PADDLE_ENFORCE(
+ platform::places_are_same_class(kernel_pair.first.place_,
+ kernel_pair.second.place_),
+ "TransDataType Only Support DataType transform on same place!");
+ auto src = in.Get();
+ auto* dst = out->GetMutable();
+ *dst = src;
+}
+
void TransDataType(const platform::DeviceContext* ctx,
const KernelTypePair& kernel_pair, const Variable& in,
Variable* out) {
@@ -121,6 +143,8 @@ std::vector NCHW2NHWC = {0, 2, 3, 1};
}
REGISTER_DATA_TRANSFORM_FN(f::KernelFP32, f::KernelFP64, f::TransDataType);
+REGISTER_DATA_TRANSFORM_FN(f::KernelPlain, f::KernelCUDNN, f::DummyTrans);
+REGISTER_DATA_TRANSFORM_FN(f::KernelCUDNN, f::KernelPlain, f::DummyTrans);
REGISTER_DATA_TRANSFORM_FN(f::KernelNHWC, f::KernelNCHW,
std::bind(f::TransDataLayout, NHWC2NCHW,
std::placeholders::_1,
diff --git a/paddle/framework/init.cc b/paddle/framework/init.cc
index 682cff168d4d31e0565fc987604f97a671566fbd..7ec8d18b0e886948f4fb951e17875584413771db 100644
--- a/paddle/framework/init.cc
+++ b/paddle/framework/init.cc
@@ -15,6 +15,7 @@ limitations under the License. */
#include
#include "paddle/framework/init.h"
+#include "paddle/framework/operator.h"
#include "paddle/platform/device_context.h"
#include "paddle/platform/place.h"
#include "paddle/string/piece.h"
@@ -24,7 +25,6 @@ namespace framework {
std::once_flag gflags_init_flag;
-// TODO(qijun) move init gflags to init.cc
void InitGflags(std::vector &argv) {
std::call_once(gflags_init_flag, [&]() {
int argc = argv.size();
@@ -72,8 +72,14 @@ bool InitDevices(const std::vector &devices) {
LOG(WARNING) << "Not specified CPU device, create CPU by Default.";
}
platform::DeviceContextPool::Init(places);
+ framework::UseALL();
return true;
}
+void InitGLOG(const std::string &prog_name) {
+ google::InitGoogleLogging(prog_name.c_str());
+ google::InstallFailureSignalHandler();
+}
+
} // namespace framework
} // namespace paddle
diff --git a/paddle/framework/init.h b/paddle/framework/init.h
index 33907f9eb00fb3469b53dcf8151557cc7a2d3791..9c84a03ded52632047841f95badbcf44bc9f48d1 100644
--- a/paddle/framework/init.h
+++ b/paddle/framework/init.h
@@ -22,6 +22,8 @@ namespace framework {
void InitGflags(std::vector &argv);
+void InitGLOG(const std::string &prog_name);
+
bool InitDevices(const std::vector &devices);
} // namespace framework
diff --git a/paddle/framework/library_type.h b/paddle/framework/library_type.h
index 7707799cae8c4edc304cd81725270a85f01fd28d..1e3084835439b0d55de72a669b93acbaef7ed6b9 100644
--- a/paddle/framework/library_type.h
+++ b/paddle/framework/library_type.h
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
+#include
namespace paddle {
namespace framework {
@@ -41,6 +42,9 @@ inline std::string LibraryTypeToString(const LibraryType& library_type) {
inline LibraryType StringToLibraryType(const char* ctype) {
std::string s(ctype);
+ for (size_t i = 0; i < s.size(); ++i) {
+ s[i] = toupper(s[i]);
+ }
if (s == std::string("PLAIN")) {
return LibraryType::kPlain;
} else if (s == std::string("MKLDNN")) {
diff --git a/paddle/framework/lod_tensor.cc b/paddle/framework/lod_tensor.cc
index 7b6dc09bdb5535488c8c4dbc71c9cd6a7998bd0b..ef85ed69dbe87d4b5b1b1b5d6a04220a5266a635 100644
--- a/paddle/framework/lod_tensor.cc
+++ b/paddle/framework/lod_tensor.cc
@@ -43,6 +43,22 @@ std::ostream &operator<<(std::ostream &os, const LoD &lod) {
return os;
}
+std::ostream &operator<<(std::ostream &os, const LoDTensor &t) {
+ PADDLE_ENFORCE(platform::is_cpu_place(t.place()));
+ PADDLE_ENFORCE(t.type().hash_code() == typeid(float).hash_code());
+
+ os << "dim: " << t.dims() << "\n";
+ os << "lod: " << t.lod() << "\n";
+
+ // only print first ten elements
+ int64_t size = t.numel() < 10 ? t.numel() : 10;
+ for (int64_t i = 0; i < size; ++i) {
+ os << t.data()[i] << " ";
+ }
+
+ return os;
+}
+
LoD SliceLevels(const LoD &in, size_t level_begin, size_t level_end) {
LoD new_lod;
new_lod.reserve(level_end - level_begin);
@@ -177,6 +193,9 @@ void AppendLoD(LoD *lod, const LoD &lod_length) {
lod->empty() || lod->size() == lod_length.size(),
"The lod_length should has the same size with the appended lod.");
if (lod->empty()) {
+ for (size_t i = 0; i < lod_length.size(); ++i) {
+ lod->emplace_back(1, 0); // size = 1, value = 0;
+ }
*lod = LoD(lod_length.size(), std::vector({0}));
}
for (size_t i = 0; i < lod->size(); ++i) {
@@ -214,9 +233,10 @@ void SerializeToStream(std::ostream &os, const LoDTensor &tensor,
SerializeToStream(os, static_cast(tensor), dev_ctx);
}
-void DeserializeFromStream(std::istream &is, LoDTensor *tensor) {
+void DeserializeFromStream(std::istream &is, LoDTensor *tensor,
+ const platform::DeviceContext &dev_ctx) {
{
- // the 1st field, unit32_t version for SelectedRows
+ // the 1st field, unit32_t version for LoDTensor
uint32_t version;
is.read(reinterpret_cast(&version), sizeof(version));
PADDLE_ENFORCE_EQ(version, 0U, "Only version 0 is supported");
@@ -237,7 +257,71 @@ void DeserializeFromStream(std::istream &is, LoDTensor *tensor) {
}
}
// the 3st filed, Tensor
- DeserializeFromStream(is, static_cast(tensor));
+ DeserializeFromStream(is, static_cast(tensor), dev_ctx);
+}
+
+std::vector LoDTensor::SplitLoDTensor(
+ const std::vector places) const {
+ check_memory_size();
+ // PADDLE_ENFORCE(lod().empty() || (lod().size() == 1 && lod()[0].empty())
+ // , "Disable parallel lod for now");
+ PADDLE_ENFORCE(lod().empty(), "Disable parallel lod for now");
+ PADDLE_ENFORCE(dims()[0] % places.size() == 0,
+ "Batch size should be divided by places size");
+
+ std::vector lods;
+ for (size_t place_idx = 0; place_idx < places.size(); ++place_idx) {
+ size_t begin = place_idx * dims()[0] / places.size();
+ size_t end = (place_idx + 1) * dims()[0] / places.size();
+ auto src = Slice(static_cast(begin), static_cast(end));
+
+ LoDTensor dst;
+ dst.Resize(src.dims());
+ auto &dst_place = places[place_idx];
+ auto dst_ptr = dst.mutable_data(dst_place, src.type());
+
+ // TODO(tonyyang-svail):
+ // change the following to framework::CopyFrom
+ auto src_place = src.place();
+ auto src_ptr = src.data();
+ auto size = src.numel() * SizeOfType(src.type());
+ if (platform::is_cpu_place(src_place) &&
+ platform::is_cpu_place(dst_place)) {
+ memory::Copy(boost::get(dst_place), dst_ptr,
+ boost::get(src_place), src_ptr, size);
+ } else {
+ PADDLE_THROW("Not Implemented");
+ }
+
+ lods.emplace_back(dst);
+ }
+
+ return lods;
+}
+
+void LoDTensor::MergeLoDTensor(
+ const std::vector &lod_tensors, platform::Place place) {
+ PADDLE_ENFORCE(platform::is_cpu_place(place));
+ PADDLE_ENFORCE(!lod_tensors.empty());
+
+ framework::DDim new_dim = lod_tensors[0]->dims();
+ std::type_index new_type = lod_tensors[0]->type();
+ for (auto *lod : lod_tensors) {
+ PADDLE_ENFORCE(new_dim == lod->dims());
+ PADDLE_ENFORCE(new_type == lod->type());
+ PADDLE_ENFORCE(platform::is_cpu_place(lod->place()));
+ }
+ new_dim[0] *= lod_tensors.size();
+ Resize(new_dim);
+
+ auto *dst_ptr = reinterpret_cast(mutable_data(place, new_type));
+ for (auto *src : lod_tensors) {
+ auto size = src->numel() * SizeOfType(src->type());
+ memory::Copy(boost::get(place), dst_ptr,
+ boost::get(src->place()),
+ src->data(), size);
+ dst_ptr += size;
+ }
}
} // namespace framework
diff --git a/paddle/framework/lod_tensor.h b/paddle/framework/lod_tensor.h
index 147db3ab0877662d9e47ae7ee6df05638b5fcbd1..b27936c1986d13d33786035adeebe14da92b190e 100644
--- a/paddle/framework/lod_tensor.h
+++ b/paddle/framework/lod_tensor.h
@@ -58,6 +58,7 @@ using Vector = thrust::host_vector<
using LoD = std::vector>;
std::ostream& operator<<(std::ostream& os, const LoD& lod);
+std::ostream& operator<<(std::ostream& os, const LoDTensor& t);
/*
* Slice levels from a LoD.
@@ -144,6 +145,12 @@ class LoDTensor : public Tensor {
*/
void ShrinkInLevel(size_t level, size_t elem_begin, size_t elem_end);
+ std::vector SplitLoDTensor(
+ const std::vector places) const;
+
+ void MergeLoDTensor(const std::vector& lod_tensors,
+ platform::Place place);
+
private:
LoD lod_;
};
@@ -208,7 +215,8 @@ void AppendLoD(LoD* lod, const LoD& lod_length);
*/
void SerializeToStream(std::ostream& os, const LoDTensor& tensor,
const platform::DeviceContext& dev_ctx);
-void DeserializeFromStream(std::istream& is, LoDTensor* tensor);
+void DeserializeFromStream(std::istream& is, LoDTensor* tensor,
+ const platform::DeviceContext& dev_ctx);
} // namespace framework
} // namespace paddle
diff --git a/paddle/framework/lod_tensor_test.cc b/paddle/framework/lod_tensor_test.cc
index 0747c8db531d6ae443d76591b945cce0c9bbea2b..0868c1f6e695b8de6a755f167951f404de0942ca 100644
--- a/paddle/framework/lod_tensor_test.cc
+++ b/paddle/framework/lod_tensor_test.cc
@@ -132,7 +132,7 @@ TEST_F(LoDTensorTester, SerializeAndDeserialize) {
std::ostringstream oss;
SerializeToStream(oss, lod_tensor_, cpu_ctx);
std::istringstream iss(oss.str());
- DeserializeFromStream(iss, &dst_tensor);
+ DeserializeFromStream(iss, &dst_tensor, cpu_ctx);
float* dst_ptr = dst_tensor.mutable_data(platform::CPUPlace());
for (int i = 0; i < kLodTensorSize; ++i) {
EXPECT_EQ(dst_ptr[i], i);
diff --git a/paddle/framework/op_desc.cc b/paddle/framework/op_desc.cc
index 3e58e6442edfe006c8aed238f67b9524783601ee..e02e572af2c4ee122d033bc7b5231be94026c180 100644
--- a/paddle/framework/op_desc.cc
+++ b/paddle/framework/op_desc.cc
@@ -64,7 +64,7 @@ class CompileTimeInferShapeContext : public InferShapeContext {
PADDLE_ENFORCE_EQ(in_var->GetType(), proto::VarDesc::LOD_TENSOR,
"The %d-th output of Output(%s) must be LoDTensor.", j,
out);
- out_var->SetLoDLevel(in_var->GetLodLevel());
+ out_var->SetLoDLevel(in_var->GetLoDLevel());
}
bool IsRuntime() const override;
diff --git a/paddle/framework/op_kernel_type.h b/paddle/framework/op_kernel_type.h
index b06002096fb109da806809f7b908d9768cf095ba..053897784c1c4350deadf39e2a009220d38f65f9 100644
--- a/paddle/framework/op_kernel_type.h
+++ b/paddle/framework/op_kernel_type.h
@@ -26,13 +26,12 @@ namespace framework {
struct OpKernelType {
struct Hash {
size_t operator()(const OpKernelType& key) const {
- int place = key.place_.which() + (1 << LEFT_SHIFT);
- int data_type =
- static_cast(key.data_type_) + (1 << (LEFT_SHIFT + 1));
- int data_layout =
- static_cast(key.data_layout_) + (1 << (LEFT_SHIFT + 2));
- int library_type =
- static_cast(key.library_type_) + (1 << (LEFT_SHIFT + 3));
+ int place = key.place_.which();
+ int data_type = static_cast(key.data_type_) << LEFT_SHIFT;
+ int data_layout = static_cast(key.data_layout_) << (LEFT_SHIFT * 2);
+ int library_type = static_cast(key.library_type_)
+ << (LEFT_SHIFT * 3);
+
std::hash hasher;
return hasher(place + data_type + data_layout + library_type);
}
diff --git a/paddle/framework/op_registry_test.cc b/paddle/framework/op_registry_test.cc
index cef530c6e639f6e2188869fa57d114ec6b885aa8..a286925bbe4cc455a5956b4ac1800a2bafa3bfdb 100644
--- a/paddle/framework/op_registry_test.cc
+++ b/paddle/framework/op_registry_test.cc
@@ -12,13 +12,16 @@
See the License for the specific language governing permissions and
limitations under the License. */
-#include "paddle/framework/op_registry.h"
+#include
#include
+#include "paddle/framework/op_registry.h"
+
namespace pd = paddle::framework;
namespace paddle {
namespace framework {
+
class CosineOp : public OperatorBase {
public:
using OperatorBase::OperatorBase;
@@ -252,7 +255,6 @@ TEST(OperatorRegistrar, CPU) {
op->Run(scope, cpu_place);
}
-#ifdef PADDLE_WITH_CUDA
TEST(OperatorRegistrar, CUDA) {
paddle::framework::proto::OpDesc op_desc;
paddle::platform::CUDAPlace cuda_place(0);
@@ -263,4 +265,131 @@ TEST(OperatorRegistrar, CUDA) {
op->Run(scope, cuda_place);
}
-#endif
+
+static int op_test_value = 0;
+
+using paddle::platform::DeviceContext;
+using paddle::platform::CPUDeviceContext;
+using paddle::platform::CUDADeviceContext;
+
+namespace paddle {
+namespace framework {
+
+class OpWithMultiKernelTest : public OperatorWithKernel {
+ public:
+ using OperatorWithKernel::OperatorWithKernel;
+
+ protected:
+ void InferShape(InferShapeContext* ctx) const override {}
+
+ framework::OpKernelType GetActualKernelType(
+ const framework::ExecutionContext& ctx) const override {
+ return framework::OpKernelType(proto::DataType::FP32, ctx.device_context());
+ }
+
+ framework::OpKernelType GetExpectedKernelType(
+ const framework::OpKernelType& kernel) const override {
+ return framework::OpKernelType(kernel.data_type_, platform::CUDAPlace(0),
+ kernel.data_layout_,
+ framework::LibraryType::kCUDNN);
+ }
+};
+
+template
+class OpMultiKernelTest : public paddle::framework::OpKernel {
+ public:
+ void Compute(const paddle::framework::ExecutionContext& ctx) const;
+};
+
+template
+class OpMultiKernelTest
+ : public paddle::framework::OpKernel {
+ public:
+ void Compute(const paddle::framework::ExecutionContext& ctx) const {
+ ++op_test_value;
+ }
+};
+
+template
+class OpMultiKernelTest
+ : public paddle::framework::OpKernel {
+ public:
+ void Compute(const paddle::framework::ExecutionContext& ctx) const {
+ --op_test_value;
+ }
+};
+
+template
+class OpMultiKernelTest2 : public paddle::framework::OpKernel {
+ public:
+ void Compute(const paddle::framework::ExecutionContext& ctx) const;
+};
+
+template
+class OpMultiKernelTest2
+ : public paddle::framework::OpKernel {
+ public:
+ void Compute(const paddle::framework::ExecutionContext& ctx) const {
+ op_test_value += 10;
+ }
+};
+
+template
+class OpMultiKernelTest2
+ : public paddle::framework::OpKernel {
+ public:
+ void Compute(const paddle::framework::ExecutionContext& ctx) const {
+ op_test_value -= 10;
+ }
+};
+
+} // namespace framework
+} // namespace paddle
+
+REGISTER_OP_WITHOUT_GRADIENT(op_with_multi_kernel,
+ paddle::framework::OpWithMultiKernelTest,
+ paddle::framework::OpKernelTestMaker);
+REGISTER_OP_KERNEL(
+ op_with_multi_kernel, CPU, paddle::platform::CPUPlace,
+ paddle::framework::OpMultiKernelTest);
+REGISTER_OP_KERNEL(
+ op_with_multi_kernel, MKLDNN, paddle::platform::CPUPlace,
+ paddle::framework::OpMultiKernelTest2);
+REGISTER_OP_KERNEL(
+ op_with_multi_kernel, CUDA, paddle::platform::CUDAPlace,
+ paddle::framework::OpMultiKernelTest);
+REGISTER_OP_KERNEL(
+ op_with_multi_kernel, CUDNN, paddle::platform::CUDAPlace,
+ paddle::framework::OpMultiKernelTest2);
+
+TEST(OperatorRegistrar, OpWithMultiKernel) {
+ paddle::framework::proto::OpDesc op_desc;
+ paddle::platform::CUDAPlace cuda_place(0);
+ paddle::platform::CPUPlace cpu_place;
+ paddle::framework::Scope scope;
+
+ op_desc.set_type("op_with_multi_kernel");
+ auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
+
+ // use all available kernels
+ paddle::framework::UseALL();
+ op->Run(scope, cuda_place);
+ EXPECT_EQ(op_test_value, -10);
+
+ // remove cuda kernels
+ paddle::framework::UseCPU();
+ op->Run(scope, cpu_place);
+
+ EXPECT_EQ(op_test_value, -9);
+
+ // add cuda kernels
+ paddle::framework::UseCUDA();
+ op->Run(scope, cuda_place);
+
+ EXPECT_EQ(op_test_value, -10);
+
+ // use cudnn kernel
+ paddle::framework::UseCUDNN();
+ op->Run(scope, cuda_place);
+ EXPECT_EQ(op_test_value, -20);
+}
diff --git a/paddle/framework/operator.cc b/paddle/framework/operator.cc
index fc7091f1c89f8b3f998f6d1b68f032b76bad2197..b9dcf16da5d5e1d10176b6c9ae6ea1be080064ae 100644
--- a/paddle/framework/operator.cc
+++ b/paddle/framework/operator.cc
@@ -11,6 +11,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
+#include
#include
#include
@@ -25,6 +26,53 @@ limitations under the License. */
namespace paddle {
namespace framework {
+std::vector> kKernelPriority;
+
+void UseCPU() {
+ kKernelPriority.clear();
+ /*Plain CPU*/
+ auto pair0 = std::make_tuple(platform::CPUPlace(), LibraryType::kPlain);
+ kKernelPriority.insert(kKernelPriority.begin(), pair0);
+}
+
+void UseMKLDNN() {
+ UseCPU();
+#if PADDLE_WITH_MKLML
+ {
+ /*MKLDNN Kernel*/
+ auto pair0 = std::make_tuple(platform::CPUPlace(), LibraryType::kMKLDNN);
+ kKernelPriority.insert(kKernelPriority.begin(), pair0);
+ }
+#endif
+}
+
+void UseCUDA() {
+ UseMKLDNN();
+#if PADDLE_WITH_CUDA
+ /*Plain GPU*/
+ auto pair0 = std::make_tuple(platform::CUDAPlace(0), LibraryType::kPlain);
+ kKernelPriority.insert(kKernelPriority.begin(), pair0);
+#endif
+}
+
+void UseCUDNN() {
+ UseCUDA();
+#if PADDLE_WITH_CUDA
+ if (platform::dynload::HasCUDNN()) {
+ /*CUDNN Kernel*/
+ auto pair0 = std::make_tuple(platform::CUDAPlace(0), LibraryType::kCUDNN);
+ kKernelPriority.insert(kKernelPriority.begin(), pair0);
+ }
+#endif
+}
+
+void UseALL() {
+ UseCPU();
+ UseMKLDNN();
+ UseCUDA();
+ UseCUDNN();
+}
+
std::string OperatorBase::Input(const std::string& name) const {
auto& ins = Inputs(name);
PADDLE_ENFORCE_LE(ins.size(), 1UL,
@@ -185,7 +233,8 @@ static const Tensor* GetTensorFromVar(const Variable* var) {
} else if (var->IsType()) {
t = &(var->Get().value());
} else {
- PADDLE_THROW("Variable type must be LoDTensor/SelectedRows.");
+ PADDLE_THROW("Variable type_id %s, expect LoDTensor/SelectedRows.",
+ var->Type().name());
}
return t;
}
@@ -197,7 +246,8 @@ static Tensor* GetMutableTensorFromVar(Variable* var) {
} else if (var->IsType()) {
t = var->GetMutable()->mutable_value();
} else {
- PADDLE_THROW("Variable type must be LoDTensor/SelectedRows.");
+ PADDLE_THROW("Variable type_id %s, expect LoDTensor/SelectedRows.",
+ var->Type().name());
}
return t;
}
@@ -359,7 +409,8 @@ class RuntimeInferShapeContext : public InferShapeContext {
} else if (var->IsType()) {
return var->Get().GetCompleteDims();
} else {
- PADDLE_THROW("Variable type must be LoDTensor/SelectedRows.");
+ PADDLE_THROW("Variable %s type_id %s, expect LoDTensor/SelectedRows.",
+ name, var->Type().name());
}
}
@@ -370,7 +421,8 @@ class RuntimeInferShapeContext : public InferShapeContext {
} else if (var->IsType()) {
var->GetMutable()->set_height(dim[0]);
} else {
- PADDLE_THROW("Variable type must be LoDTensor/SelectedRows.");
+ PADDLE_THROW("Variable %s type_id %s, expect LoDTensor/SelectedRows.",
+ name, var->Type().name());
}
}
@@ -402,6 +454,12 @@ const platform::DeviceContext* GetDeviceContext(
}
}
+const platform::DeviceContext* GetDeviceContext(
+ const framework::OpKernelType& kernel) {
+ platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
+ return pool.Get(kernel.place_);
+}
+
void OperatorWithKernel::Run(const Scope& scope,
const platform::Place& place) const {
RuntimeInferShapeContext infer_shape_ctx(*this, scope);
@@ -422,13 +480,8 @@ void OperatorWithKernel::Run(const Scope& scope,
ExecutionContext ctx(*this, scope, *dev_ctx);
auto actual_kernel_key = GetActualKernelType(ctx);
- auto expected_kernel_key = GetExpectedKernelType(actual_kernel_key);
- auto kernel_iter = kernels.find(expected_kernel_key);
- if (kernel_iter == kernels.end()) {
- PADDLE_THROW("The operator %s does not support %s", type_,
- expected_kernel_key);
- }
+ auto expected_kernel_key = GetExpectedKernelType(actual_kernel_key);
if (actual_kernel_key == expected_kernel_key) {
PADDLE_ENFORCE_EQ(actual_kernel_key.place_, expected_kernel_key.place_,
@@ -436,9 +489,24 @@ void OperatorWithKernel::Run(const Scope& scope,
"CPU and other devices. For example, multi-GPU model "
"parallelism will failed.");
} else {
+ // find the best key candidate
+ const DataTransformFnMap& trans_map = DataTransformFnMap::Instance();
+ for (auto& candidate : kKernelPriority) {
+ auto candidate_key =
+ OpKernelType(actual_kernel_key.data_type_, std::get<0>(candidate),
+ actual_kernel_key.data_layout_, std::get<1>(candidate));
+
+ auto candidate_pair = std::make_pair(actual_kernel_key, candidate_key);
+ if ((actual_kernel_key == candidate_key) ||
+ (kernels.count(candidate_key) &&
+ trans_map.GetNullable(candidate_pair))) {
+ expected_kernel_key = candidate_key;
+ break;
+ }
+ }
+
auto kernel_pair = std::make_pair(actual_kernel_key, expected_kernel_key);
- const DataTransformFn* trans_fun =
- DataTransformFnMap::Instance().GetNullable(kernel_pair);
+ const DataTransformFn* trans_fun = trans_map.GetNullable(kernel_pair);
if (trans_fun) {
auto input_vars = this->InputVars();
// TODO(qijun) filter the input vars that do not need to be transformed
@@ -471,7 +539,20 @@ void OperatorWithKernel::Run(const Scope& scope,
}
}
- kernel_iter->second->Compute(ctx);
+ VLOG(10) << "Actual kernel: " << actual_kernel_key
+ << "Expected kernel: " << expected_kernel_key;
+
+ auto kernel_iter = kernels.find(expected_kernel_key);
+
+ if (kernel_iter == kernels.end()) {
+ PADDLE_THROW("The operator %s does not support %s", type_,
+ expected_kernel_key);
+ }
+
+ auto* expected_dev_ctx = GetDeviceContext(expected_kernel_key);
+ ExecutionContext expected_ctx(*this, scope, *expected_dev_ctx);
+
+ kernel_iter->second->Compute(expected_ctx);
}
OpKernelType OperatorWithKernel::GetActualKernelType(
diff --git a/paddle/framework/operator.h b/paddle/framework/operator.h
index d0a9b643d565d6651fd7ec0b515f088362852ba3..1f5a4af58c5a9ad2fa8f4ac08ece67084b8f741a 100644
--- a/paddle/framework/operator.h
+++ b/paddle/framework/operator.h
@@ -17,6 +17,7 @@ limitations under the License. */
#include
#include
#include
+#include
#include
#include
@@ -52,10 +53,33 @@ constexpr char kGradVarSuffix[] = "@GRAD";
/// Variables with this suffix are supposed to be filled up with zeros.
constexpr char kZeroVarSuffix[] = "@ZERO";
-// define some kernel hint
-const std::string kUseCPU = "use_cpu";
-const std::string kUseCUDNN = "use_cudnn";
-const std::string kUseMKLDNN = "use_mkldnn";
+// define some kernel priority
+extern std::vector> kKernelPriority;
+
+/**
+ * @brief Use cpu kernel only
+ */
+void UseCPU();
+
+/**
+ * @brief Perfer MKLDNN kernel than Plain CPU kernel
+ */
+void UseMKLDNN();
+
+/**
+ * @brief Perfer CUDA kernel than Plain CPU kernel
+ */
+void UseCUDA();
+
+/**
+ * @brief Perfer cudnn kernel than Plain CUDA kernel
+ */
+void UseCUDNN();
+
+/**
+ * @brief Use all available kernels
+ */
+void UseALL();
inline std::string GradVarName(const std::string& var_name) {
return var_name + kGradVarSuffix;
diff --git a/paddle/framework/scope.cc b/paddle/framework/scope.cc
index 0c01d605bcd95f5796fba1e5a3351a2640b2898a..4e80e3d974e2b646ad62d26991e4629f8c450578 100644
--- a/paddle/framework/scope.cc
+++ b/paddle/framework/scope.cc
@@ -17,6 +17,7 @@ limitations under the License. */
#include // for unique_ptr
#include // for call_once
#include "glog/logging.h"
+#include "paddle/framework/threadpool.h"
#include "paddle/string/printf.h"
namespace paddle {
@@ -87,7 +88,8 @@ void Scope::DeleteScope(Scope* scope) {
auto it = std::find(this->kids_.begin(), this->kids_.end(), scope);
PADDLE_ENFORCE(it != this->kids_.end(), "Cannot find %p as kid scope", scope);
this->kids_.erase(it);
- delete scope;
+ // Make delete async.
+ Async([scope] { delete scope; });
}
void Scope::Rename(const std::string& origin_name,
diff --git a/paddle/framework/selected_rows.cc b/paddle/framework/selected_rows.cc
index 82adfa7123a3cf40d929021602c45fe7d2e34ffa..3b3e60177a495cc99f38ee8b82af41c4c76b8652 100644
--- a/paddle/framework/selected_rows.cc
+++ b/paddle/framework/selected_rows.cc
@@ -37,8 +37,8 @@ void SerializeToStream(std::ostream& os, const SelectedRows& selected_rows,
SerializeToStream(os, selected_rows.value(), dev_ctx);
}
-void DeserializeFromStream(std::istream& is, SelectedRows* selected_rows) {
- auto tensor = *selected_rows->mutable_value();
+void DeserializeFromStream(std::istream& is, SelectedRows* selected_rows,
+ const platform::DeviceContext& dev_ctx) {
{
// the 1st field, unit32_t version for SelectedRows
uint32_t version;
@@ -62,7 +62,7 @@ void DeserializeFromStream(std::istream& is, SelectedRows* selected_rows) {
selected_rows->set_height(height);
}
// the 4st field, tensor which contains the data
- DeserializeFromStream(is, &tensor);
+ DeserializeFromStream(is, selected_rows->mutable_value(), dev_ctx);
}
} // namespace framework
diff --git a/paddle/framework/selected_rows.h b/paddle/framework/selected_rows.h
index 699e392688e9889f050592172f8bfc45f855d0b1..30d3dfc1e89f073a8180ceacf77619b36f7079a9 100644
--- a/paddle/framework/selected_rows.h
+++ b/paddle/framework/selected_rows.h
@@ -66,7 +66,8 @@ class SelectedRows {
*/
void SerializeToStream(std::ostream& os, const SelectedRows& selected_rows,
const platform::DeviceContext& dev_ctx);
-void DeserializeFromStream(std::istream& is, SelectedRows* selected_rows);
+void DeserializeFromStream(std::istream& is, SelectedRows* selected_rows,
+ const platform::DeviceContext& dev_ctx);
} // namespace framework
} // namespace paddle
diff --git a/paddle/framework/selected_rows_test.cc b/paddle/framework/selected_rows_test.cc
index 75487c4010391aa9e519d73058184fa936dabb84..8ff3fb6a97199a2798ab29c56957a0f77fa26628 100644
--- a/paddle/framework/selected_rows_test.cc
+++ b/paddle/framework/selected_rows_test.cc
@@ -51,10 +51,12 @@ TEST_F(SelectedRowsTester, SerializeAndDeseralize) {
SerializeToStream(oss, *selected_rows_, cpu_ctx);
std::istringstream iss(oss.str());
- DeserializeFromStream(iss, &dst_tensor);
+ DeserializeFromStream(iss, &dst_tensor, cpu_ctx);
ASSERT_EQ(selected_rows_->rows(), dst_tensor.rows());
ASSERT_EQ(selected_rows_->height(), dst_tensor.height());
+ ASSERT_EQ(selected_rows_->value().dims(), dst_tensor.value().dims());
+ ASSERT_EQ(selected_rows_->GetCompleteDims(), dst_tensor.GetCompleteDims());
}
} // namespace framework
diff --git a/paddle/framework/tensor.h b/paddle/framework/tensor.h
index 341a6949beeb2dfa64b23d2079bd8f48750a94f8..02b125cbbee641c01ad34b984c634b6d95d65a8e 100644
--- a/paddle/framework/tensor.h
+++ b/paddle/framework/tensor.h
@@ -55,6 +55,8 @@ class Tensor {
template
inline const T* data() const;
+ inline void switch_place(platform::Place new_place);
+
/**
* @brief Return a pointer to mutable memory block.
* @note If not exist, then allocation.
@@ -200,6 +202,15 @@ class Tensor {
size_t offset_;
};
+inline void Tensor::switch_place(platform::Place new_place) {
+ if (holder_->place() == new_place) {
+ return;
+ }
+
+ // TODO(tonyyang-svail): do memcpy here.
+ PADDLE_THROW("Not Implemented");
+}
+
} // namespace framework
} // namespace paddle
diff --git a/paddle/framework/tensor_util.h b/paddle/framework/tensor_util.h
index 6a21f8db1e3966fd23eee0da2346b2d61f9321fb..5ac13cba4dae9c058e2d96da24dab01f44ece772 100644
--- a/paddle/framework/tensor_util.h
+++ b/paddle/framework/tensor_util.h
@@ -270,7 +270,23 @@ inline void SerializeToStream(std::ostream& os, const Tensor& tensor,
}
}
-inline void DeserializeFromStream(std::istream& is, Tensor* tensor) {
+struct DeserializedDataFunctor {
+ DeserializedDataFunctor(void** buf, Tensor* tensor,
+ const platform::Place& place)
+ : buf_(buf), tensor_(tensor), place_(place) {}
+
+ template
+ void operator()() {
+ *buf_ = tensor_->mutable_data(place_);
+ }
+
+ void** buf_;
+ Tensor* tensor_;
+ platform::Place place_;
+};
+
+inline void DeserializeFromStream(std::istream& is, Tensor* tensor,
+ const platform::DeviceContext& dev_ctx) {
uint32_t version;
is.read(reinterpret_cast(&version), sizeof(version));
PADDLE_ENFORCE_EQ(version, 0U, "Only version 0 is supported");
@@ -289,27 +305,28 @@ inline void DeserializeFromStream(std::istream& is, Tensor* tensor) {
dims.reserve(static_cast(desc.dims().size()));
std::copy(desc.dims().begin(), desc.dims().end(), std::back_inserter(dims));
tensor->Resize(framework::make_ddim(dims));
-
void* buf;
- platform::Place cpu = platform::CPUPlace();
- // TODO(Yancey1989): use VisiterDataType instead of DataType switch
- switch (desc.data_type()) {
- case proto::FP32:
- buf = tensor->mutable_data(cpu);
- break;
- case proto::FP64:
- buf = tensor->mutable_data(cpu);
- break;
- case proto::INT32:
- buf = tensor->mutable_data(cpu);
- break;
- case proto::INT64:
- buf = tensor->mutable_data(cpu);
- break;
- default:
- PADDLE_THROW("DataType %d not supported", desc.data_type());
+ auto ctx = platform::CPUDeviceContext();
+ if (platform::is_gpu_place(dev_ctx.GetPlace())) {
+#ifdef PADDLE_WITH_CUDA
+ Tensor cpu_tensor;
+ cpu_tensor.Resize(framework::make_ddim(dims));
+ framework::VisitDataType(
+ desc.data_type(),
+ DeserializedDataFunctor(&buf, &cpu_tensor, ctx.GetPlace()));
+ is.read(static_cast(buf), cpu_tensor.memory_size());
+ auto cpu_place = new platform::CPUPlace();
+ framework::CopyFrom(cpu_tensor, *cpu_place, dev_ctx, tensor);
+ delete cpu_place;
+#else
+ PADDLE_THROW("Unexpected branch");
+#endif
+ } else {
+ framework::VisitDataType(
+ desc.data_type(),
+ DeserializedDataFunctor(&buf, tensor, ctx.GetPlace()));
+ is.read(static_cast(buf), tensor->memory_size());
}
- is.read(static_cast(buf), tensor->memory_size());
}
}
diff --git a/paddle/framework/tensor_util_test.cc b/paddle/framework/tensor_util_test.cc
index 0dc5166fcabf77b48b8681ab1f050e2bc88f44ab..15cd2bd09c4a34bc7a5bb8645762a3e0aaefd713 100644
--- a/paddle/framework/tensor_util_test.cc
+++ b/paddle/framework/tensor_util_test.cc
@@ -270,11 +270,12 @@ TEST(Tensor, SerializeAndDeserialize) {
SerializeToStream(oss, src_tensor, cpu_ctx);
std::istringstream iss(oss.str());
- DeserializeFromStream(iss, &dst_tensor);
+ DeserializeFromStream(iss, &dst_tensor, cpu_ctx);
int* dst_ptr = dst_tensor.mutable_data(platform::CPUPlace());
for (int i = 0; i < 5; ++i) {
ASSERT_EQ(dst_ptr[i], array[i]);
}
+ ASSERT_EQ(dst_tensor.dims(), src_tensor.dims());
delete place;
}
#ifdef PADDLE_WITH_CUDA
@@ -292,13 +293,12 @@ TEST(Tensor, SerializeAndDeserialize) {
SerializeToStream(oss, gpu_tensor, gpu_ctx);
std::istringstream iss(oss.str());
- DeserializeFromStream(iss, &dst_tensor);
+ DeserializeFromStream(iss, &dst_tensor, gpu_ctx);
int* dst_ptr = dst_tensor.mutable_data(platform::CPUPlace());
for (int i = 0; i < 6; ++i) {
ASSERT_EQ(dst_ptr[i], array[i]);
}
-
delete gpu_place;
}
#endif
diff --git a/paddle/framework/threadpool.h b/paddle/framework/threadpool.h
index bcd8190755083ec30687675602a1c95a9c15c69e..3ac345851c38557f82698786dd3bc8e1202a4256 100644
--- a/paddle/framework/threadpool.h
+++ b/paddle/framework/threadpool.h
@@ -29,7 +29,6 @@ namespace framework {
class ThreadPool {
public:
typedef std::packaged_task Task;
- typedef std::function Fun;
/**
* @brief Get a instance of threadpool, the thread number will
@@ -67,7 +66,8 @@ class ThreadPool {
* @return std::future, we could wait for the task finished by
* f.wait().
*/
- std::future Run(const Fun& fn) {
+ template
+ std::future Run(Callback fn) {
std::unique_lock lock(mutex_);
Task task(std::bind(fn));
std::future f = task.get_future();
@@ -159,5 +159,13 @@ class ThreadPool {
std::condition_variable completed_;
};
+// Run a function asynchronously.
+// NOTE: The function must return void. If the function need to return a value,
+// you can use lambda to capture a value pointer.
+template
+std::future Async(Callback callback) {
+ return ThreadPool::GetInstance()->Run(callback);
+}
+
} // namespace framework
} // namespace paddle
diff --git a/paddle/framework/var_desc.cc b/paddle/framework/var_desc.cc
index 7d002b9ea0b597730685ee03b021c4982f787f49..aeab18d7214f8d9dd79bc3d2e0322490445b3b49 100644
--- a/paddle/framework/var_desc.cc
+++ b/paddle/framework/var_desc.cc
@@ -52,7 +52,7 @@ void VarDesc::SetLoDLevel(int32_t lod_level) {
}
}
-int32_t VarDesc::GetLodLevel() const {
+int32_t VarDesc::GetLoDLevel() const {
switch (desc_.type()) {
case proto::VarDesc::LOD_TENSOR:
return desc_.lod_tensor().lod_level();
diff --git a/paddle/framework/var_desc.h b/paddle/framework/var_desc.h
index 4fd2abe7fb215c3ac454de3e30754685111eb570..fc482c467404a6b9dfed64c43871d91d3d10c766 100644
--- a/paddle/framework/var_desc.h
+++ b/paddle/framework/var_desc.h
@@ -76,7 +76,7 @@ class VarDesc {
void SetLoDLevel(int32_t lod_level);
- int32_t GetLodLevel() const;
+ int32_t GetLoDLevel() const;
proto::VarDesc::VarType GetType() const;
diff --git a/paddle/framework/var_type.h b/paddle/framework/var_type.h
index 0e6ea8dc69fe9b7fdaa1163b8d63295624cd3abc..5b7a08a08732a6ccbc206f6a4f0aa4788ce4a219 100644
--- a/paddle/framework/var_type.h
+++ b/paddle/framework/var_type.h
@@ -17,6 +17,8 @@ limitations under the License. */
#include "paddle/framework/lod_rank_table.h"
#include "paddle/framework/lod_tensor.h"
#include "paddle/framework/lod_tensor_array.h"
+#include "paddle/framework/selected_rows.h"
+#include "paddle/framework/variable.h"
namespace paddle {
namespace framework {
@@ -35,7 +37,7 @@ inline proto::VarDesc::VarType ToVarType(std::type_index type) {
}
template
-inline void VisitVarType(const Variable& var, Visitor visitor) {
+inline void VisitVarType(const framework::Variable& var, Visitor visitor) {
switch (ToVarType(var.Type())) {
case proto::VarDesc_VarType_LOD_TENSOR:
visitor(var.Get());
diff --git a/paddle/inference/CMakeLists.txt b/paddle/inference/CMakeLists.txt
new file mode 100644
index 0000000000000000000000000000000000000000..8437b2b21942ead544dab8636db1b355b7cf7bd5
--- /dev/null
+++ b/paddle/inference/CMakeLists.txt
@@ -0,0 +1,47 @@
+set(FLUID_CORE_MODULES
+ backward proto_desc paddle_memory executor prune init ${GLOB_OP_LIB})
+
+cc_library(paddle_fluid_api
+ SRCS inference.cc
+ DEPS ${FLUID_CORE_MODULES})
+
+# Merge all modules into a simgle static library
+cc_library(paddle_fluid DEPS paddle_fluid_api ${FLUID_CORE_MODULES})
+
+# ptools
+# just for testing, we may need to change the storing format for inference_model
+# and move the dependent of pickle.
+# download from http://www.picklingtools.com/
+# build in the C++ sub-directory, using command
+# make -f Makefile.Linux libptools.so
+set(PTOOLS_LIB)
+set(PTOOLS_ROOT $ENV{PTOOLS_ROOT} CACHE PATH "Folder contains PicklingTools")
+find_path(PTOOLS_INC_DIR chooseser.h PATHS ${PTOOLS_ROOT}/C++)
+find_library(PTOOLS_SHARED_LIB NAMES ptools PATHS ${PTOOLS_ROOT}/C++)
+if(PTOOLS_INC_DIR AND PTOOLS_SHARED_LIB)
+ add_definitions(-DPADDLE_USE_PTOOLS)
+ set(PTOOLS_LIB ptools)
+ message(STATUS "Found PicklingTools: ${PTOOLS_SHARED_LIB}")
+ add_library(${PTOOLS_LIB} SHARED IMPORTED GLOBAL)
+ set_property(TARGET ${PTOOLS_LIB} PROPERTY IMPORTED_LOCATION ${PTOOLS_SHARED_LIB})
+ include_directories(${PTOOLS_ROOT}/C++)
+ include_directories(${PTOOLS_ROOT}/C++/opencontainers_1_8_5/include)
+ add_definitions(-DOC_NEW_STYLE_INCLUDES) # used in ptools
+endif()
+
+add_executable(example example.cc)
+if(APPLE)
+ set(OPTIONAL_LINK_FLAGS)
+ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
+ set(OPTIONAL_LINK_FLAGS "-undefined dynamic_lookup")
+ endif()
+ target_link_libraries(example
+ -Wl,-force_load paddle_fluid
+ ${OPTIONAL_LINK_FLAGS}
+ ${PTOOLS_LIB})
+else()
+ target_link_libraries(example
+ -Wl,--start-group -Wl,--whole-archive paddle_fluid
+ -Wl,--no-whole-archive -Wl,--end-group
+ ${PTOOLS_LIB})
+endif()
diff --git a/paddle/inference/example.cc b/paddle/inference/example.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9711b20e6fb4099a2cc497029468ebd1fd0b3456
--- /dev/null
+++ b/paddle/inference/example.cc
@@ -0,0 +1,79 @@
+/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. */
+
+#include
+#include
+#include "gflags/gflags.h"
+#include "paddle/inference/inference.h"
+
+DEFINE_string(dirname, "", "Directory of the inference model.");
+DEFINE_string(feed_var_names, "", "Names of feeding variables");
+DEFINE_string(fetch_var_names, "", "Names of fetching variables");
+
+int main(int argc, char** argv) {
+ google::ParseCommandLineFlags(&argc, &argv, true);
+ if (FLAGS_dirname.empty() || FLAGS_feed_var_names.empty() ||
+ FLAGS_fetch_var_names.empty()) {
+ // Example:
+ // ./example --dirname=recognize_digits_mlp.inference.model
+ // --feed_var_names="x"
+ // --fetch_var_names="fc_2.tmp_2"
+ std::cout << "Usage: ./example --dirname=path/to/your/model "
+ "--feed_var_names=x --fetch_var_names=y"
+ << std::endl;
+ exit(1);
+ }
+
+ std::cout << "FLAGS_dirname: " << FLAGS_dirname << std::endl;
+ std::cout << "FLAGS_feed_var_names: " << FLAGS_feed_var_names << std::endl;
+ std::cout << "FLAGS_fetch_var_names: " << FLAGS_fetch_var_names << std::endl;
+
+ std::string dirname = FLAGS_dirname;
+ std::vector feed_var_names = {FLAGS_feed_var_names};
+ std::vector fetch_var_names = {FLAGS_fetch_var_names};
+
+ paddle::InferenceEngine* engine = new paddle::InferenceEngine();
+ engine->LoadInferenceModel(dirname, feed_var_names, fetch_var_names);
+
+ paddle::framework::LoDTensor input;
+ srand(time(0));
+ float* input_ptr =
+ input.mutable_data({1, 784}, paddle::platform::CPUPlace());
+ for (int i = 0; i < 784; ++i) {
+ input_ptr[i] = rand() / (static_cast(RAND_MAX));
+ }
+
+ std::vector feeds;
+ feeds.push_back(input);
+ std::vector fetchs;
+ engine->Execute(feeds, fetchs);
+
+ for (size_t i = 0; i < fetchs.size(); ++i) {
+ auto dims_i = fetchs[i].dims();
+ std::cout << "dims_i:";
+ for (int j = 0; j < dims_i.size(); ++j) {
+ std::cout << " " << dims_i[j];
+ }
+ std::cout << std::endl;
+ std::cout << "result:";
+ float* output_ptr = fetchs[i].data();
+ for (int j = 0; j < paddle::framework::product(dims_i); ++j) {
+ std::cout << " " << output_ptr[j];
+ }
+ std::cout << std::endl;
+ }
+
+ delete engine;
+ return 0;
+}
diff --git a/paddle/inference/inference.cc b/paddle/inference/inference.cc
new file mode 100644
index 0000000000000000000000000000000000000000..48a51efcd25afeb499ab9389c2f27c8bd2515cd8
--- /dev/null
+++ b/paddle/inference/inference.cc
@@ -0,0 +1,202 @@
+/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. */
+
+#include "inference.h"
+#include
+#include "paddle/framework/executor.h"
+#include "paddle/framework/feed_fetch_method.h"
+#include "paddle/framework/init.h"
+#include "paddle/framework/scope.h"
+
+#ifdef PADDLE_USE_PTOOLS
+#include "chooseser.h"
+#endif
+
+namespace paddle {
+
+void InferenceEngine::LoadInferenceModel(
+ const std::string& dirname,
+ const std::vector& feed_var_names,
+ const std::vector& fetch_var_names) {
+#ifdef PADDLE_USE_PTOOLS
+ std::string model_filename = dirname + "/__model__";
+ LOG(INFO) << "Using PicklingTools, loading model from " << model_filename;
+ Val v;
+ LoadValFromFile(model_filename.c_str(), v, SERIALIZE_P0);
+ std::string program_desc_str = v["program_desc_str"];
+ LOG(INFO) << "program_desc_str's size: " << program_desc_str.size();
+// PicklingTools cannot parse the vector of strings correctly.
+#else
+ // program_desc_str
+ // the inference.model is stored by following python codes:
+ // inference_program = fluid.io.get_inference_program(predict)
+ // model_filename = "recognize_digits_mlp.inference.model/inference.model"
+ // with open(model_filename, "w") as f:
+ // program_str = inference_program.desc.serialize_to_string()
+ // f.write(struct.pack('q', len(program_str)))
+ // f.write(program_str)
+ std::string model_filename = dirname + "/inference.model";
+ LOG(INFO) << "loading model from " << model_filename;
+ std::ifstream fs(model_filename, std::ios_base::binary);
+ int64_t size = 0;
+ fs.read(reinterpret_cast(&size), sizeof(int64_t));
+ LOG(INFO) << "program_desc_str's size: " << size;
+ std::string program_desc_str;
+ program_desc_str.resize(size);
+ fs.read(&program_desc_str[0], size);
+#endif
+ program_ = new framework::ProgramDesc(program_desc_str);
+ GenerateLoadProgram(dirname);
+
+ if (feed_var_names.empty() || fetch_var_names.empty()) {
+ LOG(FATAL) << "Please specify the feed_var_names and fetch_var_names.";
+ }
+ feed_var_names_ = feed_var_names;
+ fetch_var_names_ = fetch_var_names;
+ PrependFeedOp();
+ AppendFetchOp();
+}
+
+bool InferenceEngine::IsParameter(const framework::VarDesc* var) {
+ if (var->Persistable()) {
+ // There are many unreachable variables in the program
+ for (size_t i = 0; i < program_->Size(); ++i) {
+ const framework::BlockDesc& block = program_->Block(i);
+ for (auto* op : block.AllOps()) {
+ for (auto input_argument_name : op->InputArgumentNames()) {
+ if (input_argument_name == var->Name()) {
+ return true;
+ }
+ }
+ }
+ }
+ }
+ return false;
+}
+
+void InferenceEngine::GenerateLoadProgram(const std::string& dirname) {
+ framework::BlockDesc* global_block = program_->MutableBlock(0);
+
+ load_program_ = new framework::ProgramDesc();
+ framework::BlockDesc* load_block = load_program_->MutableBlock(0);
+ for (auto* var : global_block->AllVars()) {
+ if (IsParameter(var)) {
+ LOG(INFO) << "parameter's name: " << var->Name();
+
+ framework::VarDesc* new_var = load_block->Var(var->Name());
+ new_var->SetShape(var->Shape());
+ new_var->SetDataType(var->GetDataType());
+ new_var->SetType(var->GetType());
+ new_var->SetLoDLevel(var->GetLoDLevel());
+ new_var->SetPersistable(true);
+
+ // append_op
+ framework::OpDesc* op = load_block->AppendOp();
+ op->SetType("load");
+ op->SetOutput("Out", {new_var->Name()});
+ op->SetAttr("file_path", {dirname + "/" + new_var->Name()});
+ op->CheckAttrs();
+ }
+ }
+}
+
+void InferenceEngine::PrependFeedOp() {
+ if (!program_) {
+ LOG(FATAL) << "Please initialize the program_ first.";
+ }
+
+ framework::BlockDesc* global_block = program_->MutableBlock(0);
+
+ // create_var
+ framework::VarDesc* feed_var = global_block->Var("feed");
+ feed_var->SetType(framework::proto::VarDesc::FEED_MINIBATCH);
+ feed_var->SetPersistable(true);
+
+ // prepend feed_op
+ for (size_t i = 0; i < feed_var_names_.size(); ++i) {
+ std::string var_name = feed_var_names_[i];
+ LOG(INFO) << "feed var's name: " << var_name;
+
+ // prepend_op
+ framework::OpDesc* op = global_block->PrependOp();
+ op->SetType("feed");
+ op->SetInput("X", {"feed"});
+ op->SetOutput("Out", {var_name});
+ op->SetAttr("col", {static_cast(i)});
+ op->CheckAttrs();
+ }
+}
+
+void InferenceEngine::AppendFetchOp() {
+ if (!program_) {
+ LOG(FATAL) << "Please initialize the program_ first.";
+ }
+
+ framework::BlockDesc* global_block = program_->MutableBlock(0);
+
+ // create_var
+ framework::VarDesc* fetch_var = global_block->Var("fetch");
+ fetch_var->SetType(framework::proto::VarDesc::FETCH_LIST);
+ fetch_var->SetPersistable(true);
+
+ // append fetch_op
+ for (size_t i = 0; i < fetch_var_names_.size(); ++i) {
+ std::string var_name = fetch_var_names_[i];
+ LOG(INFO) << "fetch var's name: " << var_name;
+
+ // append_op
+ framework::OpDesc* op = global_block->AppendOp();
+ op->SetType("fetch");
+ op->SetInput("X", {var_name});
+ op->SetOutput("Out", {"fetch"});
+ op->SetAttr("col", {static_cast(i)});
+ op->CheckAttrs();
+ }
+}
+
+void InferenceEngine::Execute(const std::vector& feeds,
+ std::vector& fetchs) {
+ if (!program_ || !load_program_) {
+ LOG(FATAL) << "Please initialize the program_ and load_program_ first.";
+ }
+
+ if (feeds.size() < feed_var_names_.size()) {
+ LOG(FATAL) << "Please feed " << feed_var_names_.size() << " input Tensors.";
+ }
+
+ auto* place = new platform::CPUPlace();
+ framework::InitDevices({"CPU"});
+ framework::Executor* executor = new framework::Executor(*place);
+ framework::Scope* scope = new framework::Scope();
+
+ executor->Run(*load_program_, scope, 0, true, true);
+
+ // set_feed_variable
+ for (size_t i = 0; i < feed_var_names_.size(); ++i) {
+ framework::SetFeedVariable(scope, feeds[i], "feed", i);
+ }
+
+ executor->Run(*program_, scope, 0, true, true);
+
+ // get_fetch_variable
+ fetchs.resize(fetch_var_names_.size());
+ for (size_t i = 0; i < fetch_var_names_.size(); ++i) {
+ fetchs[i] = framework::GetFetchVariable(*scope, "fetch", i);
+ }
+
+ delete place;
+ delete scope;
+ delete executor;
+}
+} // namespace paddle
diff --git a/paddle/inference/inference.h b/paddle/inference/inference.h
new file mode 100644
index 0000000000000000000000000000000000000000..a3f3ef4b440036a0b27353cc092eed1bbf96eeb3
--- /dev/null
+++ b/paddle/inference/inference.h
@@ -0,0 +1,50 @@
+/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. */
+
+#pragma once
+
+#include "paddle/framework/block_desc.h"
+#include "paddle/framework/lod_tensor.h"
+#include "paddle/framework/program_desc.h"
+
+namespace paddle {
+
+class InferenceEngine {
+public:
+ InferenceEngine() : program_(nullptr), load_program_(nullptr) {}
+ ~InferenceEngine() {
+ delete program_;
+ delete load_program_;
+ }
+
+ void LoadInferenceModel(const std::string& dirname,
+ const std::vector& feed_var_names,
+ const std::vector& fetch_var_names);
+ void Execute(const std::vector& feeds,
+ std::vector& fetchs);
+
+private:
+ bool IsParameter(const framework::VarDesc* var);
+ void GenerateLoadProgram(const std::string& dirname);
+ void PrependFeedOp();
+ void AppendFetchOp();
+
+private:
+ framework::ProgramDesc* program_;
+ framework::ProgramDesc* load_program_;
+ std::vector feed_var_names_;
+ std::vector fetch_var_names_;
+};
+
+} // namespace paddle
diff --git a/paddle/operators/CMakeLists.txt b/paddle/operators/CMakeLists.txt
index 77b52eb1760c7b79aa47efb447a4c99b6ab5e027..f1ce52332327ed2a9f290ccf412199fd5a6bbb67 100644
--- a/paddle/operators/CMakeLists.txt
+++ b/paddle/operators/CMakeLists.txt
@@ -152,6 +152,7 @@ op_library(conv_transpose_op DEPS vol2col)
op_library(gru_op DEPS sequence2batch gru_compute)
op_library(recurrent_op DEPS executor)
op_library(cos_sim_op DEPS cos_sim_functor)
+op_library(parallel_do_op DEPS executor)
# FIXME(typhoonzero): save/load depends lodtensor serialization functions
op_library(save_op DEPS lod_tensor)
op_library(load_op DEPS lod_tensor)
diff --git a/paddle/operators/activation_op.h b/paddle/operators/activation_op.h
index 0885f7c570b9b52dc51597347295734fd689da8d..88c3d1c597a853abdee7753a5110be4a1726e905 100644
--- a/paddle/operators/activation_op.h
+++ b/paddle/operators/activation_op.h
@@ -15,6 +15,7 @@ limitations under the License. */
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
+#include "paddle/operators/detail/safe_ref.h"
namespace paddle {
namespace operators {
@@ -26,12 +27,16 @@ class ActivationKernel
using T = typename Functor::ELEMENT_TYPE;
void Compute(const framework::ExecutionContext& context) const override {
- auto* X = context.Input("X");
- auto* Out = context.Output("Out");
- Out->mutable_data(context.GetPlace());
-
- auto x = framework::EigenVector::Flatten(*X);
- auto out = framework::EigenVector::Flatten(*Out);
+ auto& X = detail::Ref(context.Input("X"),
+ "Cannot get input tensor X, variable name = %s",
+ context.op().Input("X"));
+
+ auto& Out = detail::Ref(context.Output("Out"),
+ "Cannot get output tensor Out, variable name = %s",
+ context.op().Output("Out"));
+ Out.mutable_data(context.GetPlace());
+ auto x = framework::EigenVector::Flatten(X);
+ auto out = framework::EigenVector::Flatten(Out);
auto* place =
context.template device_context().eigen_device();
Functor functor;
diff --git a/paddle/operators/adagrad_op.h b/paddle/operators/adagrad_op.h
index 0d77dbcbacd4efb6c1900e57b5c4ea9e9b136771..66f5b0f449a4f11a3c734c98a6a97833763348a1 100644
--- a/paddle/operators/adagrad_op.h
+++ b/paddle/operators/adagrad_op.h
@@ -47,8 +47,7 @@ class AdagradOpKernel : public framework::OpKernel {
*ctx.Input("Grad"));
auto moment = framework::EigenVector::Flatten(
*ctx.Input("Moment"));
- auto lr = framework::EigenVector::Flatten(
- *ctx.Input("LearningRate"));
+ auto* learning_rate = ctx.Input("LearningRate");
auto param_out = framework::EigenVector::Flatten(*param_out_tensor);
auto moment_out = framework::EigenVector::Flatten(*moment_out_tensor);
@@ -56,8 +55,16 @@ class AdagradOpKernel : public framework::OpKernel {
moment_out.device(*place) = moment + grad * grad;
Eigen::DSizes m_dsize(moment_out_tensor->numel());
- param_out.device(*place) =
- param - lr.broadcast(m_dsize) * grad / (moment_out.sqrt() + epsilon);
+ if (platform::is_cpu_place(ctx.GetPlace())) {
+ auto* lr = learning_rate->data();
+ param_out.device(*place) =
+ param - lr[0] * grad / (moment_out.sqrt() + epsilon);
+ } else {
+ auto lr = framework::EigenVector::Flatten(*learning_rate);
+ param_out.device(*place) =
+ param -
+ lr.broadcast(m_dsize) * grad / (moment_out.sqrt() + epsilon);
+ }
} else if (grad_var->IsType()) {
auto* param_tensor = ctx.Input("Param");
PADDLE_ENFORCE_EQ(param_tensor, param_out_tensor);
diff --git a/paddle/operators/batch_norm_op.cc b/paddle/operators/batch_norm_op.cc
index 98db28ddee7c6cdb37fe7732649d4fc38de7b873..dd7b038b00813b192177c05dc06aa165a60b5156 100644
--- a/paddle/operators/batch_norm_op.cc
+++ b/paddle/operators/batch_norm_op.cc
@@ -64,7 +64,7 @@ class BatchNormOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE(x_dims.size() >= 2 && x_dims.size() <= 5,
"Input X must have 2 to 5 dimensions.");
- const int C =
+ const int64_t C =
(data_layout == DataLayout::kNCHW ? x_dims[1]
: x_dims[x_dims.size() - 1]);
@@ -78,6 +78,7 @@ class BatchNormOp : public framework::OperatorWithKernel {
ctx->SetOutputDim("VarianceOut", {C});
ctx->SetOutputDim("SavedMean", {C});
ctx->SetOutputDim("SavedVariance", {C});
+ ctx->ShareLoD("X", "Y");
}
};
diff --git a/paddle/operators/conv_cudnn_op.cu.cc b/paddle/operators/conv_cudnn_op.cu.cc
index 0aa7dd48cafc3e2387ac902882d84ce9029cfcd0..0c5ed3e4e80304c6fd174975166804347feb18b1 100644
--- a/paddle/operators/conv_cudnn_op.cu.cc
+++ b/paddle/operators/conv_cudnn_op.cu.cc
@@ -315,10 +315,7 @@ class CudnnConvGradOpKernel : public framework::OpKernel {
} // namespace operators
} // namespace paddle
-REGISTER_OP_KERNEL(conv2d, CUDNN, paddle::platform::CUDAPlace,
- paddle::operators::CudnnConvOpKernel,
- paddle::operators::CudnnConvOpKernel);
-
+// TODO(dzhwinter) : below register should be removed
REGISTER_OP_CUDA_KERNEL(conv2d_cudnn,
paddle::operators::CudnnConvOpKernel,
paddle::operators::CudnnConvOpKernel);
diff --git a/paddle/operators/conv_op.cc b/paddle/operators/conv_op.cc
index e65a5dce52c3c51d3d6bee1684c1e97230203d38..ad84524e1785e3b6b4586c83001852b6dba7afe8 100644
--- a/paddle/operators/conv_op.cc
+++ b/paddle/operators/conv_op.cc
@@ -44,14 +44,12 @@ void ConvOp::InferShape(framework::InferShapeContext* ctx) const {
paddings.size(), strides.size(),
"Conv paddings dimension and Conv strides dimension should be the same.");
- int input_channels = in_dims[1];
- PADDLE_ENFORCE_EQ(input_channels, filter_dims[1] * groups,
+ PADDLE_ENFORCE_EQ(in_dims[1], filter_dims[1] * groups,
"The number of input channels should be equal to filter "
"channels * groups.");
- int output_channels = filter_dims[0];
PADDLE_ENFORCE_EQ(
- output_channels % groups, 0,
+ filter_dims[0] % groups, 0,
"The number of output channels should be divided by groups.");
std::vector output_shape({in_dims[0], filter_dims[0]});
@@ -66,6 +64,7 @@ void ConvOp::InferShape(framework::InferShapeContext* ctx) const {
dilations[i], paddings[i], strides[i]));
}
ctx->SetOutputDim("Output", framework::make_ddim(output_shape));
+ ctx->ShareLoD("Input", "Output");
}
Conv2DOpMaker::Conv2DOpMaker(OpProto* proto, OpAttrChecker* op_checker)
diff --git a/paddle/operators/conv_op.h b/paddle/operators/conv_op.h
index 83786e2329e7ae3c2908fdfdaeb1f79d19a53f47..fe3c0bc9302257d444c7431c40c8ab7e4c1fe0e7 100644
--- a/paddle/operators/conv_op.h
+++ b/paddle/operators/conv_op.h
@@ -62,12 +62,25 @@ class ConvOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override;
+ framework::OpKernelType GetExpectedKernelType(
+ const framework::OpKernelType& kernel) const override {
+ return framework::OpKernelType(kernel.data_type_, platform::CUDAPlace(0),
+ kernel.data_layout_,
+ framework::LibraryType::kCUDNN);
+ }
};
class ConvOpGrad : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override;
+
+ framework::OpKernelType GetExpectedKernelType(
+ const framework::OpKernelType& kernel) const override {
+ return framework::OpKernelType(kernel.data_type_, platform::CUDAPlace(0),
+ kernel.data_layout_,
+ framework::LibraryType::kCUDNN);
+ }
};
template
diff --git a/paddle/operators/detail/recv_impl.cc b/paddle/operators/detail/recv_impl.cc
index b746f9df4640bac7b5c4e83091f2c2ab67e778be..319404e56a5f3c407f313991240bbbb85fd39a2a 100644
--- a/paddle/operators/detail/recv_impl.cc
+++ b/paddle/operators/detail/recv_impl.cc
@@ -21,14 +21,9 @@ namespace detail {
Status SendRecvServerImpl::SendVariable(ServerContext *context,
const VariableMessage *in_var,
VoidMessage *out_var) {
- // TODO(typhoonzero): support different variable types.
- std::istringstream iss(in_var->serialized());
- framework::LoDTensor t;
- framework::DeserializeFromStream(iss, &t);
- TensorWithName tensor_with_name =
- std::make_pair(in_var->varname(), std::move(t));
-
- var_recv_queue_.Push(std::move(tensor_with_name));
+ MessageWithName msg_with_name =
+ std::make_pair(in_var->varname(), std::move(*in_var));
+ var_recv_queue_.Push(std::move(msg_with_name));
return Status::OK;
}
@@ -37,14 +32,8 @@ Status SendRecvServerImpl::GetVariable(ServerContext *context,
VariableMessage *out_var) {
std::string get_var_name = in_var->varname();
auto *var = scope_->FindVar(get_var_name);
- auto tensor = var->Get();
- std::ostringstream oss;
- framework::SerializeToStream(oss, tensor, platform::CPUDeviceContext());
- std::string *varname = out_var->mutable_varname();
- *varname = get_var_name;
- std::string *serialized = out_var->mutable_serialized();
- *serialized = oss.str();
+ SerializeToMessage(get_var_name, var, platform::CPUDeviceContext(), out_var);
return Status::OK;
}
diff --git a/paddle/operators/detail/send_impl.cc b/paddle/operators/detail/send_impl.cc
index a812fcf39bc19a6e06dc594c13076696e7949372..ae85cf2cec2cd8e046c0c7fd3408f2212f225819 100644
--- a/paddle/operators/detail/send_impl.cc
+++ b/paddle/operators/detail/send_impl.cc
@@ -27,14 +27,8 @@ bool RPCClient::SendVariable(const framework::Scope& scope,
auto ctx = platform::CPUDeviceContext();
auto* var = scope.FindVar(inname);
PADDLE_ENFORCE(var);
- // TODO(typhoonzero): support SelectedRows
- PADDLE_ENFORCE(var->IsType(),
- "Only support LoDTensor, %s has wrong type", inname);
- const framework::LoDTensor& tensor = var->Get();
- std::ostringstream oss;
- framework::SerializeToStream(oss, tensor, ctx);
- msg.set_varname(inname);
- msg.set_serialized(oss.str());
+ SerializeToMessage(inname, var, ctx, &msg);
+
Status status = stub_->SendVariable(&context, msg, &out_msg);
if (!status.ok()) {
LOG(ERROR) << "gRPC error: " << status.error_message();
@@ -50,19 +44,15 @@ bool RPCClient::GetVariable(const framework::Scope& scope,
call_msg.set_varname(outname);
auto ctx = platform::CPUDeviceContext();
Status status = stub_->GetVariable(&context, call_msg, &ret_msg);
+ auto* outvar = scope.FindVar(outname);
if (!status.ok()) {
LOG(ERROR) << "gRPC error: " << status.error_message();
return false;
}
std::istringstream iss(ret_msg.serialized());
+ DeserializeFromMessage(ret_msg, ctx, outvar);
- framework::LoDTensor ret_tensor;
- framework::DeserializeFromStream(iss, &ret_tensor);
- auto* outvar = scope.FindVar(outname);
- framework::LoDTensor* out_tensor = outvar->GetMutable();
- // FIXME(typhoonzero): do not copy.
- framework::CopyFrom(ret_tensor, ctx.GetPlace(), ctx, out_tensor);
return true;
}
diff --git a/paddle/operators/detail/send_recv.proto b/paddle/operators/detail/send_recv.proto
index 95c8e708986eac2a27ece70864efd7eac5f84ce8..f141c755ce14ef540aeab32c11c289179aff3f8c 100644
--- a/paddle/operators/detail/send_recv.proto
+++ b/paddle/operators/detail/send_recv.proto
@@ -1,7 +1,6 @@
-/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
+/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. Licensed under
+the Apache License, Version 2.0 (the "License"); you may not use this file
+except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
@@ -13,7 +12,6 @@ See the License for the specific language governing permissions and
limitations under the License. */
syntax = "proto3";
-
package sendrecv;
service SendRecvService {
@@ -29,12 +27,18 @@ service SendRecvService {
// VariableMessage is serialized paddle variable message.
// It can be:
-// Tensor
// LoDTensor
// SelectedRows
+enum VarType {
+ LOD_TENSOR = 0;
+ SELECTED_ROWS = 1;
+}
+
message VariableMessage {
string varname = 1;
- bytes serialized = 2;
+ // TODO(Yancey1989): reference framework::proto::VarDesc::VarType
+ VarType type = 2;
+ bytes serialized = 3;
}
message VoidMessage {}
diff --git a/paddle/operators/detail/send_recv_impl.h b/paddle/operators/detail/send_recv_impl.h
index 47f730f7ae897096fbdd23a55252448cf6655fb4..1fe54f1f0536aed7d41bbdeeca076534abafe98d 100644
--- a/paddle/operators/detail/send_recv_impl.h
+++ b/paddle/operators/detail/send_recv_impl.h
@@ -14,10 +14,10 @@ limitations under the License. */
#pragma once
-#include "paddle/framework/data_type.h"
#include "paddle/framework/lod_tensor.h"
#include "paddle/framework/scope.h"
#include "paddle/framework/selected_rows.h"
+#include "paddle/framework/var_type.h"
#include "paddle/operators/detail/simple_block_queue.h"
#include "paddle/operators/detail/send_recv.grpc.pb.h"
@@ -44,7 +44,7 @@ namespace paddle {
namespace operators {
namespace detail {
-typedef std::pair TensorWithName;
+typedef std::pair MessageWithName;
class SendRecvServerImpl final : public SendRecvService::Service {
public:
@@ -60,13 +60,13 @@ class SendRecvServerImpl final : public SendRecvService::Service {
void Done();
void SetScope(framework::Scope *scope) { scope_ = scope; };
- const TensorWithName Get() { return this->var_recv_queue_.Pop(); }
+ const MessageWithName Get() { return this->var_recv_queue_.Pop(); }
- void Push(const TensorWithName &msg) { this->var_recv_queue_.Push(msg); }
+ void Push(const MessageWithName &msg) { this->var_recv_queue_.Push(msg); }
private:
// received variable from RPC, operators fetch variable from this queue.
- SimpleBlockQueue var_recv_queue_;
+ SimpleBlockQueue var_recv_queue_;
framework::Scope *scope_;
// condition of the sub program
std::mutex mutex_;
@@ -89,6 +89,53 @@ class RPCClient {
std::unique_ptr stub_;
};
+inline void SerializeToMessage(const std::string &name,
+ const framework::Variable *var,
+ const platform::DeviceContext &ctx,
+ VariableMessage *msg) {
+ msg->set_varname(name);
+ std::ostringstream oss;
+ switch (framework::ToVarType(var->Type())) {
+ case framework::proto::VarDesc_VarType_LOD_TENSOR:
+ msg->set_type(sendrecv::VarType::LOD_TENSOR);
+ framework::SerializeToStream(oss, var->Get(), ctx);
+ break;
+ case framework::proto::VarDesc_VarType_SELECTED_ROWS:
+ msg->set_type(sendrecv::VarType::SELECTED_ROWS);
+ framework::SerializeToStream(oss, var->Get(),
+ ctx);
+ break;
+ default: {
+ PADDLE_THROW("Serialize does not support type: %s",
+ typeid(var->Type()).name());
+ break;
+ }
+ }
+ msg->set_serialized(oss.str());
+}
+
+inline void DeserializeFromMessage(const VariableMessage &msg,
+ const platform::DeviceContext &ctx,
+ framework::Variable *var) {
+ using namespace paddle::framework::proto;
+ std::istringstream iss(msg.serialized());
+ switch (msg.type()) {
+ case sendrecv::VarType::LOD_TENSOR:
+ DeserializeFromStream(iss, var->GetMutable(), ctx);
+ break;
+ case sendrecv::VarType::SELECTED_ROWS: {
+ DeserializeFromStream(iss, var->GetMutable(),
+ ctx);
+ break;
+ }
+ default: {
+ PADDLE_THROW("Deserialize does not support type: %s",
+ typeid(var->Type()).name());
+ break;
+ }
+ }
+}
+
} // namespace detail
} // namespace operators
} // namespace paddle
diff --git a/paddle/operators/load_op.cc b/paddle/operators/load_op.cc
index 08b972a233aab8596a5ce7f74ea903df3b8ef0f2..7f551f101f3b3c097f664cc3e9240c2cee7f6830 100644
--- a/paddle/operators/load_op.cc
+++ b/paddle/operators/load_op.cc
@@ -38,10 +38,10 @@ class LoadOp : public framework::OperatorBase {
out_var_name);
auto *tensor = out_var->GetMutable();
- DeserializeFromStream(fin, tensor);
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
auto &dev_ctx = *pool.Get(place);
+ DeserializeFromStream(fin, tensor, dev_ctx);
if (platform::is_gpu_place(place)) {
// copy CPU to GPU
diff --git a/paddle/operators/parallel_do_op.cc b/paddle/operators/parallel_do_op.cc
new file mode 100644
index 0000000000000000000000000000000000000000..077245cd83b2535b45927c8350ca7bc14c541d21
--- /dev/null
+++ b/paddle/operators/parallel_do_op.cc
@@ -0,0 +1,293 @@
+/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. */
+
+#include
+
+#include "paddle/framework/executor.h"
+#include "paddle/framework/op_registry.h"
+#include "paddle/framework/threadpool.h"
+
+namespace paddle {
+namespace operators {
+
+static constexpr char kInputs[] = "inputs";
+static constexpr char kParameters[] = "parameters";
+static constexpr char kPlaces[] = "places";
+
+static constexpr char kOutputs[] = "outputs";
+static constexpr char kParallelScopes[] = "parallel_scopes";
+
+static constexpr char kParallelBlock[] = "sub_block";
+
+// using ParallelScopeVar = std::vector;
+using LoDTensor = framework::LoDTensor;
+using OperatorBase = framework::OperatorBase;
+
+void SplitTensorAndMoveTensorToScopes(
+ const framework::Scope &scope,
+ const std::vector &sub_scopes,
+ const std::vector &places,
+ const std::vector &names) {
+ for (auto &argu : names) {
+ auto *var = scope.FindVar(argu);
+ const auto &tensor = var->Get();
+ auto lod_tensors = tensor.SplitLoDTensor(places);
+
+ for (auto &lod : lod_tensors) {
+ VLOG(3) << lod.dims();
+ }
+
+ for (size_t i = 0; i < sub_scopes.size(); ++i) {
+ *sub_scopes[i]->Var(argu)->GetMutable() = lod_tensors[i];
+ }
+ }
+}
+
+class ParallelDoOp : public framework::OperatorBase {
+ public:
+ ParallelDoOp(const std::string &type,
+ const framework::VariableNameMap &inputs,
+ const framework::VariableNameMap &outputs,
+ const framework::AttributeMap &attrs)
+ : OperatorBase(type, inputs, outputs, attrs) {}
+
+ void Run(const framework::Scope &scope,
+ const platform::Place &place) const override {
+ // get device context from pool
+ platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
+ auto &dev_ctx = *pool.Get(place);
+
+ auto *block = Attr(kParallelBlock);
+ auto *program = block->Program();
+
+ // TODO(tonyyang-svail): get places from input
+ std::vector places;
+ places.emplace_back(platform::CPUPlace());
+ places.emplace_back(platform::CPUPlace());
+
+ auto &sub_scopes = *scope.FindVar(Output(kParallelScopes))
+ ->GetMutable>();
+ for (size_t place_idx = 0; place_idx < places.size(); ++place_idx) {
+ sub_scopes.push_back(&scope.NewScope());
+ }
+
+ SplitTensorAndMoveTensorToScopes(scope, sub_scopes, places,
+ Inputs(kInputs));
+
+ std::vector> workers;
+ workers.reserve(places.size());
+ for (size_t place_idx = 0; place_idx < places.size(); ++place_idx) {
+ VLOG(3) << "Run " << place_idx;
+
+ auto &place = places[place_idx];
+ auto *cur_scope = sub_scopes[place_idx];
+
+ // copy parameter
+ // some version of boost lacks != for boost::variant
+ if (!(dev_ctx.GetPlace() == place)) {
+ PADDLE_THROW("Not Implemented");
+ }
+
+ workers.emplace_back(framework::Async([program, cur_scope, place, block] {
+ framework::Executor executor(place);
+ executor.Run(*program, cur_scope, block->ID(),
+ false /*create_local_scope*/);
+ }));
+ }
+ for (auto &worker : workers) {
+ worker.wait();
+ }
+
+ // merge output
+ for (auto &o_name : Outputs(kOutputs)) {
+ std::vector lod_tensors;
+ lod_tensors.reserve(sub_scopes.size());
+ for (auto *sub_scope : sub_scopes) {
+ lod_tensors.emplace_back(&sub_scope->FindVar(o_name)->Get());
+ }
+
+ auto *lod_tensor_to_be_merged =
+ scope.FindVar(o_name)->GetMutable();
+ lod_tensor_to_be_merged->MergeLoDTensor(lod_tensors, dev_ctx.GetPlace());
+ }
+ }
+};
+
+class ParallelDoOpProtoMaker : public framework::OpProtoAndCheckerMaker {
+ public:
+ ParallelDoOpProtoMaker(OpProto *proto, framework::OpAttrChecker *op_checker)
+ : OpProtoAndCheckerMaker(proto, op_checker) {
+ AddInput(kInputs, "").AsDuplicable();
+ AddInput(kParameters, "").AsDuplicable();
+ AddInput(kPlaces, "");
+ AddOutput(kOutputs, "").AsDuplicable();
+ AddOutput(kParallelScopes, "");
+ AddAttr(kParallelBlock, "");
+ AddComment(R"DOC(
+ParallelDo Operator.
+)DOC");
+ }
+};
+
+class ParallelDoGradOp : public OperatorBase {
+ public:
+ ParallelDoGradOp(const std::string &type,
+ const framework::VariableNameMap &inputs,
+ const framework::VariableNameMap &outputs,
+ const framework::AttributeMap &attrs)
+ : OperatorBase(type, inputs, outputs, attrs) {}
+
+ void Run(const framework::Scope &scope,
+ const platform::Place &place) const override {
+ // // get device context from pool
+ // platform::DeviceContextPool &pool =
+ // platform::DeviceContextPool::Instance();
+ // auto &dev_ctx = *pool.Get(place);
+
+ auto *block = Attr(kParallelBlock);
+ auto *program = block->Program();
+
+ auto &sub_scopes = scope.FindVar(Input(kParallelScopes))
+ ->Get>();
+
+ // TODO(tonyyang-svail): get places from input
+ std::vector places;
+ places.emplace_back(platform::CPUPlace());
+ places.emplace_back(platform::CPUPlace());
+
+ // feed output@grad
+ SplitTensorAndMoveTensorToScopes(scope, sub_scopes, places,
+ Inputs(framework::GradVarName(kOutputs)));
+
+ for (auto &s : Inputs(framework::GradVarName(kOutputs))) {
+ VLOG(3) << s;
+ VLOG(3) << scope.FindVar(s)->Get();
+ for (auto *sub_scope : sub_scopes) {
+ VLOG(3) << sub_scope->FindVar(s)->Get();
+ }
+ }
+
+ // exe run
+ std::vector> workers;
+ for (size_t place_idx = 0; place_idx < places.size(); ++place_idx) {
+ VLOG(3) << "Run " << place_idx;
+
+ auto &place = places[place_idx];
+ auto *cur_scope = sub_scopes[place_idx];
+
+ // execute
+ workers.emplace_back(framework::Async([program, cur_scope, place, block] {
+ framework::Executor executor(place);
+ executor.Run(*program, cur_scope, block->ID(),
+ false /*create_local_scope*/);
+ }));
+ }
+ for (auto &worker : workers) {
+ worker.wait();
+ }
+
+ // merge grad
+ for (auto &s : Outputs(framework::GradVarName(kParameters))) {
+ VLOG(3) << s;
+
+ auto &t = sub_scopes[0]->FindVar(s)->Get();
+ VLOG(3) << t;
+
+ std::string s_buf = s + "@BUF";
+ auto *t_buf = sub_scopes[0]->Var(s_buf)->GetMutable();
+
+ for (size_t place_idx = 1; place_idx < places.size(); ++place_idx) {
+ auto &tt = sub_scopes[place_idx]->FindVar(s)->Get();
+ VLOG(3) << place_idx;
+ VLOG(3) << tt;
+ framework::CopyFrom(tt, places[0], t_buf);
+
+ auto sum_op = framework::OpRegistry::CreateOp(
+ "sum", {{"X", {s, s_buf}}}, {{"Out", {s}}},
+ framework::AttributeMap{});
+ sum_op->Run(*sub_scopes[0], place);
+ }
+
+ VLOG(3) << t;
+ framework::CopyFrom(t, place, scope.FindVar(s)->GetMutable());
+ }
+ }
+};
+
+class ParallelDoGradOpDescMaker : public framework::SingleGradOpDescMaker {
+ public:
+ using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
+
+ protected:
+ virtual std::unique_ptr Apply() const {
+ auto *grad = new framework::OpDesc();
+ grad->SetType("parallel_do_grad");
+ for (auto &input_param : this->InputNames()) {
+ VLOG(3) << input_param;
+ grad->SetInput(input_param, this->Input(input_param));
+ grad->SetOutput(framework::GradVarName(input_param),
+ this->InputGrad(input_param, false));
+ }
+
+ for (auto &output_param : this->OutputNames()) {
+ if (output_param == kParallelScopes) {
+ grad->SetInput(output_param, this->Output(output_param));
+ grad->SetInput(framework::GradVarName(output_param),
+ this->Output(output_param));
+ } else {
+ grad->SetInput(output_param, this->Output(output_param));
+ grad->SetInput(framework::GradVarName(output_param),
+ this->OutputGrad(output_param));
+ }
+ }
+ grad->SetAttrMap(this->Attrs());
+ grad->SetBlockAttr(kParallelBlock, *grad_block_[0]);
+
+ return std::unique_ptr(grad);
+ }
+};
+
+class ParallelDoGradOpShapeInference : public framework::InferShapeBase {
+ public:
+ void operator()(framework::InferShapeContext *ctx) const override {
+ std::vector input{kParameters, kInputs};
+ std::vector output{kOutputs};
+ for (auto &s : input) {
+ PADDLE_ENFORCE(ctx->HasInputs(s));
+ PADDLE_ENFORCE(ctx->HasOutputs(framework::GradVarName(s)),
+ "Cannot find the gradient variable %s",
+ framework::GradVarName(s));
+ }
+ for (auto &s : output) {
+ PADDLE_ENFORCE(ctx->HasInputs(s));
+ }
+ for (auto &s : input) {
+ ctx->SetOutputsDim(framework::GradVarName(s), ctx->GetInputsDim(s));
+ }
+ if (ctx->HasInputs(kParameters)) {
+ PADDLE_ENFORCE(ctx->HasOutputs(framework::GradVarName(kParameters)));
+ ctx->SetOutputsDim(framework::GradVarName(kParameters),
+ ctx->GetInputsDim(kParameters));
+ }
+ }
+};
+
+} // namespace operators
+} // namespace paddle
+
+REGISTER_OPERATOR(parallel_do, paddle::operators::ParallelDoOp,
+ paddle::operators::ParallelDoOpProtoMaker,
+ paddle::operators::ParallelDoGradOpDescMaker);
+REGISTER_OPERATOR(parallel_do_grad, paddle::operators::ParallelDoGradOp,
+ paddle::operators::ParallelDoGradOpShapeInference);
diff --git a/paddle/operators/pool_op.cc b/paddle/operators/pool_op.cc
index 50057eb6483e9c9e745bc07dee26a0bbbbb5a48c..d3cf5fa638c53dfdfacec153211f447a1e2fa3bf 100644
--- a/paddle/operators/pool_op.cc
+++ b/paddle/operators/pool_op.cc
@@ -58,6 +58,7 @@ void PoolOp::InferShape(framework::InferShapeContext *ctx) const {
OutputSizePool(in_x_dims[i + 2], ksize[i], paddings[i], strides[i]));
}
ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
+ ctx->ShareLoD("X", "Out");
}
void PoolOpGrad::InferShape(framework::InferShapeContext *ctx) const {
diff --git a/paddle/operators/recv_op.cc b/paddle/operators/recv_op.cc
index 322f8571cfd4341f064e8f9df512a8d74b91ed9d..82fceb3da7d396bcfc1d95baccc4ee36b87f4d39 100644
--- a/paddle/operators/recv_op.cc
+++ b/paddle/operators/recv_op.cc
@@ -60,7 +60,7 @@ class RecvOp : public framework::OperatorBase {
}
void Stop() override {
- detail::TensorWithName term_msg;
+ detail::MessageWithName term_msg;
term_msg.first = LISTEN_TERMINATE_MESSAGE;
rpc_service_->Push(term_msg);
rpc_server_->Shutdown();
@@ -94,7 +94,7 @@ class RecvOp : public framework::OperatorBase {
// the gradient arrives, just add suffix 0~n then average the gradient.
for (size_t i = 0; i < param_count * trainer_count; ++i) {
// blocking get one var from client.
- const detail::TensorWithName &v = rpc_service_->Get();
+ const detail::MessageWithName &v = rpc_service_->Get();
auto grad_var_name = v.first;
if (grad_var_name == LISTEN_TERMINATE_MESSAGE) {
exit_flag = true;
@@ -121,11 +121,10 @@ class RecvOp : public framework::OperatorBase {
}
auto *var = recv_scope.Var(grad_var_name);
- auto *tensor = var->GetMutable();
- // FIXME(typhoonzero): do not copy
- platform::DeviceContextPool &pool = platform::DeviceContextPool::Get();
- auto &dev_ctx = *pool.Borrow(dev_place);
- framework::CopyFrom(v.second, dev_place, dev_ctx, tensor);
+ platform::DeviceContextPool &pool =
+ platform::DeviceContextPool::Instance();
+ auto &dev_ctx = *pool.Get(dev_place);
+ detail::DeserializeFromMessage(v.second, dev_ctx, var);
}
if (exit_flag) {
break;
diff --git a/paddle/operators/send_recv_op_test.cc b/paddle/operators/send_recv_op_test.cc
index 108e2dec6b3eecadd431fd25f9a31ec17a874b6b..fa94424bf9e8e719ec0822268685b0806a109d21 100644
--- a/paddle/operators/send_recv_op_test.cc
+++ b/paddle/operators/send_recv_op_test.cc
@@ -20,22 +20,27 @@ limitations under the License. */
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
#include "paddle/framework/program_desc.h"
+#include "paddle/operators/math/math_function.h"
+#include "paddle/operators/math/selected_rows_functor.h"
#include "paddle/string/printf.h"
USE_NO_KERNEL_OP(send);
USE_NO_KERNEL_OP(recv);
USE_OP(sum);
+namespace f = paddle::framework;
+namespace p = paddle::platform;
+namespace m = paddle::operators::math;
+
// global for simplicity.
-std::unique_ptr recv_op;
+std::unique_ptr recv_op;
-void InitTensorsInScope(paddle::framework::Scope &scope,
- paddle::platform::CPUPlace &place) {
- paddle::platform::CPUDeviceContext ctx(place);
+void InitTensorsInScope(f::Scope &scope, p::CPUPlace &place) {
+ p::CPUDeviceContext ctx(place);
for (int i = 0; i < 2; ++i) {
auto var_name = paddle::string::Sprintf("x%d", i);
auto var = scope.Var(var_name);
- auto tensor = var->GetMutable();
+ auto tensor = var->GetMutable();
tensor->Resize({10, 10});
float *expect = tensor->mutable_data(place);
for (int64_t i = 0; i < tensor->numel(); ++i) {
@@ -44,21 +49,53 @@ void InitTensorsInScope(paddle::framework::Scope &scope,
}
auto out_var = scope.Var("Out");
- auto out_tensor = out_var->GetMutable();
+ auto out_tensor = out_var->GetMutable();
out_tensor->Resize({10, 10});
out_tensor->mutable_data(place); // allocate
}
-void AddOp(const std::string &type,
- const paddle::framework::VariableNameMap &inputs,
- const paddle::framework::VariableNameMap &outputs,
- paddle::framework::AttributeMap attrs,
- paddle::framework::BlockDesc *block) {
+void InitSelectedRowsInScope(f::Scope &scope, p::CPUPlace &place) {
+ p::CPUDeviceContext ctx(place);
+ int64_t height = 10;
+ int64_t row_numel = 10;
+ m::SetConstant set_one;
+ // init x0
+ std::vector rows0{0, 4, 7};
+ auto x0_var = scope.Var("x0");
+ auto x0 = x0_var->GetMutable();
+ x0->set_rows(rows0);
+ x0->set_height(height);
+ auto x0_value = x0->mutable_value();
+ x0_value->mutable_data(
+ f::make_ddim({static_cast(rows0.size()), row_numel}), place);
+ set_one(ctx, x0_value, 1.0);
+
+ // init x1
+ std::vector rows1{2, 9};
+ auto x1_var = scope.Var("x1");
+ auto x1 = x1_var->GetMutable();
+ x1->set_rows(rows1);
+ x1->set_height(height);
+ auto x1_value = x1->mutable_value();
+ x1_value->mutable_data(
+ f::make_ddim({static_cast(rows1.size()), row_numel}), place);
+ set_one(ctx, x1_value, 1.0);
+
+ auto out_var = scope.Var("Out");
+ auto out = out_var->GetMutable();
+ auto out_value = out->mutable_value();
+ out->set_height(height);
+ out_value->mutable_data(f::make_ddim({5, 10}), place);
+}
+
+void AddOp(const std::string &type, const f::VariableNameMap &inputs,
+ const f::VariableNameMap &outputs, f::AttributeMap attrs,
+ f::BlockDesc *block) {
// insert output
for (auto kv : outputs) {
for (auto v : kv.second) {
auto var = block->Var(v);
- var->SetDataType(paddle::framework::proto::DataType::FP32);
+ var->SetDataType(f::proto::DataType::FP32);
}
}
@@ -74,58 +111,99 @@ void AddOp(const std::string &type,
op->SetAttrMap(attrs);
}
-void StartServerNet() {
- paddle::framework::Scope scope;
- paddle::platform::CPUPlace place;
- InitTensorsInScope(scope, place);
+void StartServerNet(bool is_sparse) {
+ f::Scope scope;
+ p::CPUPlace place;
+ if (is_sparse) {
+ InitSelectedRowsInScope(scope, place);
+ } else {
+ InitTensorsInScope(scope, place);
+ }
// sub program run in recv_op, for simple test we use sum
- paddle::framework::ProgramDesc program;
- paddle::framework::BlockDesc *block = program.MutableBlock(0);
+ f::ProgramDesc program;
+ f::BlockDesc *block = program.MutableBlock(0);
// X for server side tensors, RX for received tensers, must be of same shape.
- AddOp("sum", {{"X", {"x0", "x1"}}}, {{"Out", {"x0"}}}, {}, block);
+ AddOp("sum", {{"X", {"x0", "x1"}}}, {{"Out", {"Out"}}}, {}, block);
- paddle::framework::AttributeMap attrs;
+ f::AttributeMap attrs;
attrs.insert({"endpoint", std::string("127.0.0.1:6174")});
- attrs.insert({"ParamList", std::vector({"x0"})});
+ attrs.insert({"ParamList", std::vector({"Out"})});
attrs.insert({"GradList", std::vector({"x1"})});
std::string program_proto;
PADDLE_ENFORCE(program.Proto()->SerializeToString(&program_proto));
attrs.insert({"OptimizeProgram", program_proto});
- recv_op = paddle::framework::OpRegistry::CreateOp("recv", {{"RX", {"x1"}}},
- {}, attrs);
+ recv_op = f::OpRegistry::CreateOp("recv", {{"RX", {"x1"}}}, {}, attrs);
recv_op->Run(scope, place);
}
-TEST(SendRecvOp, CPU) {
- std::thread server_thread(StartServerNet);
- sleep(5); // wait server to start
+TEST(SendRecvOp, CPUDense) {
+ std::thread server_thread(StartServerNet, false);
+ sleep(3); // wait server to start
// local net
- paddle::framework::Scope scope;
- paddle::platform::CPUPlace place;
+ f::Scope scope;
+ p::CPUPlace place;
InitTensorsInScope(scope, place);
- paddle::framework::AttributeMap attrs;
+ f::AttributeMap attrs;
attrs.insert({"endpoints", std::vector({"127.0.0.1:6174"})});
attrs.insert({"epmap", std::vector({"127.0.0.1:6174"})});
- auto send_op = paddle::framework::OpRegistry::CreateOp(
- "send", {{"X", {"x1"}}}, {{"Out", {"x0"}}}, attrs);
+ auto send_op = f::OpRegistry::CreateOp("send", {{"X", {"x1"}}},
+ {{"Out", {"Out"}}}, attrs);
send_op->Run(scope, place);
auto in_var = scope.Var("x1");
- auto tensor = in_var->GetMutable();
+ auto tensor = in_var->GetMutable();
float *expected = tensor->data();
- auto out_var = scope.Var("x0");
- auto target = out_var->GetMutable();
+ auto out_var = scope.Var("Out");
+ auto target = out_var->GetMutable();
// x1 * 2 == x0
EXPECT_NE(target->memory_size(), size_t(0));
float *actual = target->data();
for (int64_t i = 0; i < target->numel(); ++i) {
EXPECT_EQ(expected[i] * 2, actual[i]);
}
+ recv_op->Stop();
+ server_thread.join();
+ recv_op.reset(nullptr);
+}
+TEST(SendRecvOp, CPUSparse) {
+ std::thread server_thread(StartServerNet, true);
+ sleep(3); // wait server to start
+ // local net
+ f::Scope scope;
+ p::CPUPlace place;
+ p::CPUDeviceContext ctx(place);
+ InitSelectedRowsInScope(scope, place);
+ f::AttributeMap attrs;
+ attrs.insert({"endpoints", std::vector({"127.0.0.1:6174"})});
+ attrs.insert({"epmap", std::vector({"127.0.0.1:6174"})});
+ auto send_op = f::OpRegistry::CreateOp("send", {{"X", {"x1"}}},
+ {{"Out", {"Out"}}}, attrs);
+ send_op->Run(scope, place);
+
+ auto x0 = scope.Var("x0")->GetMutable();
+ auto x1 = scope.Var("x1")->GetMutable();
+ auto out = scope.Var("Out")->GetMutable();
+ auto actual = out->mutable_value();
+
+ std::unique_ptr expect{new f::SelectedRows()};
+ auto expect_value = expect->mutable_value();
+ expect_value->mutable_data(f::make_ddim({5, 10}), place);
+
+ m::SelectedRowsAdd add_functor;
+ add_functor(ctx, *x0, *x1, expect.get());
+
+ EXPECT_EQ(actual->numel(), expect_value->numel());
+ EXPECT_EQ(out->rows().size(), x0->rows().size() + x1->rows().size());
+
+ for (int64_t i = 0; i < expect_value->numel(); ++i) {
+ EXPECT_EQ(expect_value->mutable_data(place)[i],
+ actual->mutable_data(place)[i]);
+ }
recv_op->Stop();
server_thread.join();
- // recv_op.reset();
+ recv_op.reset();
}
diff --git a/paddle/platform/device_context.cc b/paddle/platform/device_context.cc
index ea07f2e002cb76d09a11f7a5305c2d45b780e7bd..4bf643e048dcf97f7888dcc78eb1b4fa4feee046 100644
--- a/paddle/platform/device_context.cc
+++ b/paddle/platform/device_context.cc
@@ -127,15 +127,21 @@ CUDADeviceContext::CUDADeviceContext(CUDAPlace place) : place_(place) {
eigen_device_.reset(new Eigen::GpuDevice(eigen_stream_.get()));
PADDLE_ENFORCE(dynload::cublasCreate(&cublas_handle_));
PADDLE_ENFORCE(dynload::cublasSetStream(cublas_handle_, stream_));
- PADDLE_ENFORCE(dynload::cudnnCreate(&cudnn_handle_));
- PADDLE_ENFORCE(dynload::cudnnSetStream(cudnn_handle_, stream_));
+ if (dynload::HasCUDNN()) {
+ PADDLE_ENFORCE(dynload::cudnnCreate(&cudnn_handle_));
+ PADDLE_ENFORCE(dynload::cudnnSetStream(cudnn_handle_, stream_));
+ } else {
+ cudnn_handle_ = nullptr;
+ }
}
CUDADeviceContext::~CUDADeviceContext() {
SetDeviceId(place_.device);
Wait();
PADDLE_ENFORCE(dynload::cublasDestroy(cublas_handle_));
- PADDLE_ENFORCE(dynload::cudnnDestroy(cudnn_handle_));
+ if (cudnn_handle_ != nullptr) {
+ PADDLE_ENFORCE(dynload::cudnnDestroy(cudnn_handle_));
+ }
eigen_stream_.reset();
eigen_device_.reset();
PADDLE_ENFORCE(cudaStreamDestroy(stream_));
@@ -160,20 +166,6 @@ cudnnHandle_t CUDADeviceContext::cudnn_handle() const { return cudnn_handle_; }
cudaStream_t CUDADeviceContext::stream() const { return stream_; }
-CUDNNDeviceContext::CUDNNDeviceContext(CUDAPlace place)
- : CUDADeviceContext(place) {
- PADDLE_ENFORCE(dynload::cudnnCreate(&cudnn_handle_));
- PADDLE_ENFORCE(dynload::cudnnSetStream(cudnn_handle_, stream()));
-}
-
-CUDNNDeviceContext::~CUDNNDeviceContext() {
- SetDeviceId(boost::get(GetPlace()).device);
- Wait();
- PADDLE_ENFORCE(dynload::cudnnDestroy(cudnn_handle_));
-}
-
-cudnnHandle_t CUDNNDeviceContext::cudnn_handle() const { return cudnn_handle_; }
-
#endif
} // namespace platform
diff --git a/paddle/platform/device_context.h b/paddle/platform/device_context.h
index 2b366e6383d23e2d31a194edd04412892a8311eb..609ea4bd3ad50e2eefa908539339903ed1f0a807 100644
--- a/paddle/platform/device_context.h
+++ b/paddle/platform/device_context.h
@@ -103,18 +103,6 @@ struct DefaultDeviceContextType