diff --git a/.gitignore b/.gitignore
index 1512c1438e9e0b0b7b6e0c273a24b273cb652b04..020d3f0c303f7d850f4ec9c0efe58ab2d57dce2e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,11 +21,10 @@ third_party/
cmake-build-*
# generated while compiling
-python/paddle/v2/framework/core.so
+python/paddle/v2/fluid/core.so
paddle/pybind/pybind.h
CMakeFiles
cmake_install.cmake
paddle/.timestamp
python/paddlepaddle.egg-info/
paddle/pybind/pybind.h
-python/paddle/v2/framework/tests/tmp/*
diff --git a/benchmark/paddle/image/run_mkldnn.sh b/benchmark/paddle/image/run_mkldnn.sh
index a4527e04968cf8c8c3c31d16f50bc3e28381f6d8..3cc779b48d082985f75ab1c053fbe262bc6d58aa 100755
--- a/benchmark/paddle/image/run_mkldnn.sh
+++ b/benchmark/paddle/image/run_mkldnn.sh
@@ -1,9 +1,7 @@
set -e
function train() {
- unset OMP_NUM_THREADS MKL_NUM_THREADS
- export OMP_DYNAMIC="FALSE"
- export KMP_AFFINITY="granularity=fine,compact,0,0"
+ unset OMP_NUM_THREADS MKL_NUM_THREADS OMP_DYNAMIC KMP_AFFINITY
topology=$1
layer_num=$2
bs=$3
@@ -14,8 +12,6 @@ function train() {
elif [ $4 == "False" ]; then
thread=`nproc`
# each trainer_count use only 1 core to avoid conflict
- export OMP_NUM_THREADS=1
- export MKL_NUM_THREADS=1
log="logs/${topology}-${layer_num}-${thread}mklml-${bs}.log"
else
echo "Wrong input $3, use True or False."
diff --git a/cmake/external/openblas.cmake b/cmake/external/openblas.cmake
index 05d83ad58ef8485d36829e7aeede79f625cfdc43..324e29f931ecbb6beab2d363daa01a19b1a56b3e 100644
--- a/cmake/external/openblas.cmake
+++ b/cmake/external/openblas.cmake
@@ -98,7 +98,7 @@ IF(NOT ${CBLAS_FOUND})
ENDIF()
INSTALL(CODE "execute_process(
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CBLAS_INSTALL_DIR}/lib
- destination ${CMAKE_INSTALL_PREFIX}/${TMP_INSTALL_DIR}
+ ${CMAKE_INSTALL_PREFIX}/${TMP_INSTALL_DIR}
)"
)
INSTALL(CODE "MESSAGE(STATUS \"Installing: \"
diff --git a/doc/design/evaluator.md b/doc/design/evaluator.md
new file mode 100644
index 0000000000000000000000000000000000000000..a62d75ffef14962aec8c7587e172d78dfe0cb4be
--- /dev/null
+++ b/doc/design/evaluator.md
@@ -0,0 +1,58 @@
+## Evaluator Design
+
+### The Problem
+
+During training or serving, we provide the evaluation function to measure the model performance, e.g., accuracy, precision. In the operator based framework design, the data go through the network pipeline batch by batch. As a result, inside the operator, we only can calculate one minibatch metrics. We need to provide a mechanism to calculate the metrics for each N pass/batch the user wanted.
+
+### Evaluator Design
+Currently, every operation is expressed in the graph. we divide the evaluator process into three steps.
+
+1. Initialize the metric state and add it into the block.
+
+2. Calculate the statistic of the metric state in every mini-batch. The single operator is only responsible for calculating necessary statistics for one mini-batch. For example, accuracy operator only calculate a minibatch data if run once.
+
+
+3. Merge the mini-batch statistics to form the evaluation result for multiple mini-batches. When it comes to distributed training/Multi-GPU training, aggregate the value from different devices.
+
+### Implementation
+This design is shown in python API.
+Each metric operator need to caculate the metric statistic and return the batch aware states, Python side responsible for accumulate the states for each pass.
+
+
+```python
+class Evaluator(object):
+ """
+ Evaluator Base class.
+ """
+ def __init__(self, name, **kwargs):
+ """
+ Different evaluator may has different metric states. E.g, Accuracy need two variables, total and right sample counts.
+ Auc need four variables, `true_positives`,
+ `true_negatives`, `false_positives` and `false_negatives`. So every evaluator should create its needed variables and append to main_program
+
+ The initialization of Evaluator should be responsible for:
+ create metric states and append to the main_program
+ """
+ pass
+
+ def _update_ops(self, input, label, **kwargs)
+ """
+ Add mini-batch evaluator caculate operators to the main_program.
+ Add increment operator to accumulate the metric states.
+ """
+
+
+ def reset(self, executor, reset_program=None):
+ """
+ Reset metric states at the begin of each pass/user specified batch number.
+ Execute the reset_program to reset the states.
+ """
+
+
+ def eval(self, executor, eval_program=None):
+ """
+ Merge the mini-batch statistics to form the evaluation result for multiple mini-batches.
+ Execute the eval_program and return the result.
+ """
+ return eval_result
+```
diff --git a/doc/design/ops/images/2_level_rnn.dot b/doc/design/ops/images/2_level_rnn.dot
index a498e882a3d85a33d44dbad7474fa2a340e33976..5d77865061ca7bbbfcf254dd938f09aef5553505 100644
--- a/doc/design/ops/images/2_level_rnn.dot
+++ b/doc/design/ops/images/2_level_rnn.dot
@@ -1,6 +1,6 @@
digraph G {
- rnn [label="1-th level RNN" shape=box]
+ rnn [label="1st level RNN" shape=box]
subgraph cluster0 {
label = "time step 0"
@@ -8,7 +8,7 @@ digraph G {
sent0 [label="sentence"]
sent1 [label="sentence"]
- rnn1 [label="2-th level RNN" shape=box]
+ rnn1 [label="2nd level RNN" shape=box]
sent0 -> rnn1
sent1 -> rnn1
@@ -20,7 +20,7 @@ digraph G {
sent2 [label="sentence"]
sent3 [label="sentence"]
- rnn2 [label="2-th level RNN" shape=box]
+ rnn2 [label="2nd level RNN" shape=box]
sent2 -> rnn2
sent3 -> rnn2
@@ -32,7 +32,7 @@ digraph G {
sent4 [label="sentence"]
sent5 [label="sentence"]
- rnn3 [label="2-th level RNN" shape=box]
+ rnn3 [label="2nd level RNN" shape=box]
sent4 -> rnn3
sent5 -> rnn3
diff --git a/doc/design/ops/rnn.md b/doc/design/ops/rnn.md
index a78eea7d45e9e9553d153170aa31da55ec6e8289..2f4854793fa1f0b02e4dc17b51a48a972be61c06 100644
--- a/doc/design/ops/rnn.md
+++ b/doc/design/ops/rnn.md
@@ -1,62 +1,62 @@
# RNNOp design
-This document is about an RNN operator which requires that instances in a mini-batch have the same length. We will have a more flexible RNN operator.
+This document describes the RNN (Recurrent Neural Network) operator and how it is implemented in PaddlePaddle. The RNN op requires that all instances in a mini-batch have the same length. We will have a more flexible dynamic RNN operator in the future.
## RNN Algorithm Implementation
-
+
The above diagram shows an RNN unrolled into a full network.
-There are several important concepts:
+There are several important concepts here:
-- *step-net*: the sub-graph to run at each step,
-- *memory*, $h_t$, the state of the current step,
-- *ex-memory*, $h_{t-1}$, the state of the previous step,
-- *initial memory value*, the ex-memory of the first step.
+- *step-net*: the sub-graph that runs at each step.
+- *memory*, $h_t$, the state of the current step.
+- *ex-memory*, $h_{t-1}$, the state of the previous step.
+- *initial memory value*, the memory of the first (initial) step.
### Step-scope
-There could be local variables defined in step-nets. PaddlePaddle runtime realizes these variables in *step-scopes* -- scopes created for each step.
+There could be local variables defined in each step-net. PaddlePaddle runtime realizes these variables in *step-scopes* which are created for each step.
-
+

-Figure 2 the RNN's data flow
+Figure 2 illustrates the RNN's data flow
-Please be aware that all steps run the same step-net. Each step
+Please be aware that every step runs the same step-net. Each step does the following:
-1. creates the step-scope,
-2. realizes local variables, including step-outputs, in the step-scope, and
-3. runs the step-net, which could use these variables.
+1. Creates the step-scope.
+2. Initializes the local variables including step-outputs, in the step-scope.
+3. Runs the step-net, which uses the above mentioned variables.
-The RNN operator will compose its output from step outputs in step scopes.
+The RNN operator will compose its output from step outputs in each of the step scopes.
### Memory and Ex-memory
-Let's give more details about memory and ex-memory via a simply example:
+Let's give more details about memory and ex-memory using a simple example:
$$
h_t = U h_{t-1} + W x_t
$$,
-where $h_t$ and $h_{t-1}$ are the memory and ex-memory of step $t$'s respectively.
+where $h_t$ and $h_{t-1}$ are the memory and ex-memory (previous memory) of step $t$ respectively.
-In the implementation, we can make an ex-memory variable either "refers to" the memory variable of the previous step,
-or copy the value of the previous memory value to the current ex-memory variable.
+In the implementation, we can make an ex-memory variable either "refer to" the memory variable of the previous step,
+or copy the memory value of the previous step to the current ex-memory variable.
### Usage in Python
For more information on Block, please refer to the [design doc](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/block.md).
-We can define an RNN's step-net using Block:
+We can define an RNN's step-net using a Block:
```python
import paddle as pd
-X = some_op() # x is some operator's output, and is a LoDTensor
+X = some_op() # x is some operator's output and is a LoDTensor
a = some_op()
# declare parameters
@@ -68,7 +68,7 @@ with rnn.stepnet():
x = rnn.add_input(X)
# declare a memory (rnn's step)
h = rnn.add_memory(init=a)
- # h.pre_state() means previous memory of rnn
+ # h.pre_state(), the previous memory of rnn
new_state = pd.add_two( pd.matmul(W, x) + pd.matmul(U, h.pre_state()))
# update current memory
h.update(new_state)
@@ -80,19 +80,19 @@ out = rnn()
Python API functions in above example:
-- `rnn.add_input` indicates the parameter is a variable that will be segmented into step-inputs.
-- `rnn.add_memory` creates a variable used as the memory.
-- `rnn.add_outputs` mark the variables that will be concatenated across steps into the RNN output.
+- `rnn.add_input`: indicates that the parameter is a variable that will be segmented into step-inputs.
+- `rnn.add_memory`: creates a variable used as the memory.
+- `rnn.add_outputs`: marks the variables that will be concatenated across steps into the RNN output.
### Nested RNN and LoDTensor
An RNN whose step-net includes other RNN operators is known as an *nested RNN*.
-For example, we could have a 2-level RNN, where the top level corresponds to paragraphs, and the lower level corresponds to sentences.
+For example, we could have a 2-level RNN, where the top level corresponds to paragraphs, and the lower level corresponds to sentences. Each step of the higher level RNN also receives an input from the corresponding step of the lower level, and additionally the output from the previous time step at the same level.
-The following figure illustrates the feeding of text into the lower level, one sentence each step, and the feeding of step outputs to the top level. The final top level output is about the whole text.
+The following figure illustrates feeding in text into the lower level, one sentence at a step, and the feeding in step outputs to the top level. The final top level output is about the whole text.
-
+
@@ -110,7 +110,7 @@ a = some_op()
# chapter_data is a set of 128-dim word vectors
# the first level of LoD is sentence
-# the second level of LoD is chapter
+# the second level of LoD is a chapter
chapter_data = pd.Variable(shape=[None, 128], type=pd.lod_tensor, level=2)
def lower_level_rnn(paragraph):
@@ -138,14 +138,14 @@ with top_level_rnn.stepnet():
pd.matmul(W0, paragraph_data) + pd.matmul(U0, h.pre_state()))
top_level_rnn.add_outputs(h)
-# just output the last step
+# output the last step
chapter_out = top_level_rnn(output_all_steps=False)
```
-in above example, the construction of the `top_level_rnn` calls `lower_level_rnn`. The input is a LoD Tensor. The top level RNN segments input text data into paragraphs, and the lower level RNN segments each paragraph into sentences.
+In the above example, the construction of the `top_level_rnn` calls `lower_level_rnn`. The input is an LoD Tensor. The top level RNN segments input text data into paragraphs, and the lower level RNN segments each paragraph into sentences.
-By default, the `RNNOp` will concatenate the outputs from all the time steps,
-if the `output_all_steps` set to False, it will only output the final time step.
+By default, the `RNNOp` will concatenate the outputs from all the time steps.
+If the `output_all_steps` is set to False, it will only output the final time step.
diff --git a/doc/design/ops/sequence_decoder.md b/doc/design/ops/sequence_decoder.md
index 9007aae7a8355ed06c6720a921351f81b859c1fe..9db5fb8e9a9f89b004bf71ddc064cd976c0d0bee 100644
--- a/doc/design/ops/sequence_decoder.md
+++ b/doc/design/ops/sequence_decoder.md
@@ -1,35 +1,28 @@
# Design: Sequence Decoder Generating LoDTensors
-In tasks such as machine translation and image to text,
-a [sequence decoder](https://github.com/PaddlePaddle/book/blob/develop/08.machine_translation/README.md) is necessary to generate sequences.
+In tasks such as machine translation and visual captioning,
+a [sequence decoder](https://github.com/PaddlePaddle/book/blob/develop/08.machine_translation/README.md) is necessary to generate sequences, one word at a time.
This documentation describes how to implement the sequence decoder as an operator.
## Beam Search based Decoder
-The [beam search algorithm](https://en.wikipedia.org/wiki/Beam_search) is necessary when generating sequences,
-it is a heuristic search algorithm that explores the paths by expanding the most promising node in a limited set.
+The [beam search algorithm](https://en.wikipedia.org/wiki/Beam_search) is necessary when generating sequences. It is a heuristic search algorithm that explores the paths by expanding the most promising node in a limited set.
-In the old version of PaddlePaddle, a C++ class `RecurrentGradientMachine` implements the general sequence decoder based on beam search,
-due to the complexity, the implementation relays on a lot of special data structures,
-quite trivial and hard to be customized by users.
+In the old version of PaddlePaddle, the C++ class `RecurrentGradientMachine` implements the general sequence decoder based on beam search, due to the complexity involved, the implementation relies on a lot of special data structures that are quite trivial and hard to be customized by users.
-There are a lot of heuristic tricks in the sequence generation tasks,
-so the flexibility of sequence decoder is very important to users.
+There are a lot of heuristic tricks in the sequence generation tasks, so the flexibility of sequence decoder is very important to users.
-During PaddlePaddle's refactoring work,
-some new concept is proposed such as [LoDTensor](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/lod_tensor.md) and [TensorArray](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/tensor_array.md) that can better support sequence usage,
-and they can help to make the implementation of beam search based sequence decoder **more transparent and modular** .
+During the refactoring of PaddlePaddle, some new concepts are proposed such as: [LoDTensor](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/lod_tensor.md) and [TensorArray](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/tensor_array.md) that can better support the sequence usage, and they can also help make the implementation of beam search based sequence decoder **more transparent and modular** .
-For example, the RNN sates, candidates IDs and probabilities of beam search can be represented as `LoDTensors`;
+For example, the RNN states, candidates IDs and probabilities of beam search can be represented all as `LoDTensors`;
the selected candidate's IDs in each time step can be stored in a `TensorArray`, and `Packed` to the sentences translated.
## Changing LoD's absolute offset to relative offsets
-The current `LoDTensor` is designed to store levels of variable-length sequences,
-it stores several arrays of integers each represents a level.
+The current `LoDTensor` is designed to store levels of variable-length sequences. It stores several arrays of integers where each represents a level.
-The integers in each level represents the begin and end (not inclusive) offset of a sequence **in the underlying tensor**,
-let's call this format the **absolute-offset LoD** for clear.
+The integers in each level represent the begin and end (not inclusive) offset of a sequence **in the underlying tensor**,
+let's call this format the **absolute-offset LoD** for clarity.
-The relative-offset LoD can fast retrieve any sequence but fails to represent empty sequences, for example, a two-level LoD is as follows
+The relative-offset LoD can retrieve any sequence very quickly but fails to represent empty sequences, for example, a two-level LoD is as follows
```python
[[0, 3, 9]
[0, 2, 3, 3, 3, 9]]
@@ -41,10 +34,9 @@ The first level tells that there are two sequences:
while on the second level, there are several empty sequences that both begin and end at `3`.
It is impossible to tell how many empty second-level sequences exist in the first-level sequences.
-There are many scenarios that relay on empty sequence representation,
-such as machine translation or image to text, one instance has no translations or the empty candidate set for a prefix.
+There are many scenarios that rely on empty sequence representation, for example in machine translation or visual captioning, one instance has no translation or the empty candidate set for a prefix.
-So let's introduce another format of LoD,
+So let's introduce another format of LoD,
it stores **the offsets of the lower level sequences** and is called **relative-offset** LoD.
For example, to represent the same sequences of the above data
@@ -54,19 +46,18 @@ For example, to represent the same sequences of the above data
[0, 2, 3, 3, 3, 9]]
```
-the first level represents that there are two sequences,
+the first level represents that there are two sequences,
their offsets in the second-level LoD is `[0, 3)` and `[3, 5)`.
The second level is the same with the relative offset example because the lower level is a tensor.
It is easy to find out the second sequence in the first-level LoD has two empty sequences.
-The following demos are based on relative-offset LoD.
+The following examples are based on relative-offset LoD.
## Usage in a simple machine translation model
-Let's start from a simple machine translation model that is simplified from [machine translation chapter](https://github.com/PaddlePaddle/book/tree/develop/08.machine_translation) to draw a simple blueprint of what a sequence decoder can do and how to use it.
+Let's start from a simple machine translation model that is simplified from the [machine translation chapter](https://github.com/PaddlePaddle/book/tree/develop/08.machine_translation) to draw a blueprint of what a sequence decoder can do and how to use it.
-The model has an encoder that learns the semantic vector from a sequence,
-and a decoder which uses the sequence decoder to generate new sentences.
+The model has an encoder that learns the semantic vector from a sequence, and a decoder which uses the sequence encoder to generate new sentences.
**Encoder**
```python
@@ -117,7 +108,7 @@ def generate():
# which means there are 2 sentences to translate
# - the first sentence has 1 translation prefixes, the offsets are [0, 1)
# - the second sentence has 2 translation prefixes, the offsets are [1, 3) and [3, 6)
- # the target_word.lod is
+ # the target_word.lod is
# [[0, 1, 6]
# [0, 2, 4, 7, 9 12]]
# which means 2 sentences to translate, each has 1 and 5 prefixes
@@ -154,37 +145,36 @@ def generate():
translation_ids, translation_scores = decoder()
```
-The `decoder.beam_search` is a operator that given the candidates and the scores of translations including the candidates,
-return the result of the beam search algorithm.
+The `decoder.beam_search` is an operator that, given the candidates and the scores of translations including the candidates,
+returns the result of the beam search algorithm.
-In this way, users can customize anything on the inputs or outputs of beam search, for example, two ways to prune some translation prefixes
+In this way, users can customize anything on the input or output of beam search, for example:
-1. meke the correspondind elements in `topk_generated_scores` zero or some small values, beam_search will discard this candidate.
-2. remove some specific candidate in `selected_ids`
-3. get the final `translation_ids`, remove the translation sequence in it.
+1. Make the corresponding elements in `topk_generated_scores` zero or some small values, beam_search will discard this candidate.
+2. Remove some specific candidate in `selected_ids`.
+3. Get the final `translation_ids`, remove the translation sequence in it.
-The implementation of sequence decoder can reuse the C++ class [RNNAlgorithm](https://github.com/Superjom/Paddle/blob/68cac3c0f8451fe62a4cdf156747d6dc0ee000b3/paddle/operators/dynamic_recurrent_op.h#L30),
-so the python syntax is quite similar to a [RNN](https://github.com/Superjom/Paddle/blob/68cac3c0f8451fe62a4cdf156747d6dc0ee000b3/doc/design/block.md#blocks-with-for-and-rnnop).
+The implementation of sequence decoder can reuse the C++ class: [RNNAlgorithm](https://github.com/Superjom/Paddle/blob/68cac3c0f8451fe62a4cdf156747d6dc0ee000b3/paddle/operators/dynamic_recurrent_op.h#L30),
+so the python syntax is quite similar to that of an [RNN](https://github.com/Superjom/Paddle/blob/68cac3c0f8451fe62a4cdf156747d6dc0ee000b3/doc/design/block.md#blocks-with-for-and-rnnop).
-Both of them are two-level `LoDTensors`
+Both of them are two-level `LoDTensors`:
-- the first level represents `batch_size` of (source) sentences;
-- the second level represents the candidate ID sets for translation prefix.
+- The first level represents `batch_size` of (source) sentences.
+- The second level represents the candidate ID sets for translation prefix.
-for example, 3 source sentences to translate, and has 2, 3, 1 candidates.
+For example, 3 source sentences to translate, and has 2, 3, 1 candidates.
-Unlike an RNN, in sequence decoder, the previous state and the current state have different LoD and shape,
-a `lod_expand` operator is used to expand the LoD of the previous state to fit the current state.
+Unlike an RNN, in sequence decoder, the previous state and the current state have different LoD and shape, and an `lod_expand` operator is used to expand the LoD of the previous state to fit the current state.
-For example, the previous state
+For example, the previous state:
* LoD is `[0, 1, 3][0, 2, 5, 6]`
* content of tensor is `a1 a2 b1 b2 b3 c1`
-the current state stored in `encoder_ctx_expanded`
+the current state is stored in `encoder_ctx_expanded`:
* LoD is `[0, 2, 7][0 3 5 8 9 11 11]`
-* the content is
+* the content is
- a1 a1 a1 (a1 has 3 candidates, so the state should be copied 3 times for each candidates)
- a2 a2
- b1 b1 b1
@@ -192,54 +182,48 @@ the current state stored in `encoder_ctx_expanded`
- b3 b3
- None (c1 has 0 candidates, so c1 is dropped)
-Benefit from the relative offset LoD, empty candidate set can be represented naturally.
+The benefit from the relative offset LoD is that the empty candidate set can be represented naturally.
-the status in each time step can be stored in `TensorArray`, and `Pack`ed to a final LoDTensor, the corresponding syntax is
+The status in each time step can be stored in `TensorArray`, and `Pack`ed to a final LoDTensor. The corresponding syntax is:
```python
decoder.output(selected_ids)
decoder.output(selected_generation_scores)
```
-the `selected_ids` is the candidate ids for the prefixes,
-it will be `Packed` by `TensorArray` to a two-level `LoDTensor`,
-the first level represents the source sequences,
-the second level represents generated sequences.
+The `selected_ids` are the candidate ids for the prefixes, and will be `Packed` by `TensorArray` to a two-level `LoDTensor`, where the first level represents the source sequences and the second level represents generated sequences.
-Pack the `selected_scores` will get a `LoDTensor` that stores scores of each candidate of translations.
+Packing the `selected_scores` will get a `LoDTensor` that stores scores of each translation candidate.
-Pack the `selected_generation_scores` will get a `LoDTensor`, and each tail is the probability of the translation.
+Packing the `selected_generation_scores` will get a `LoDTensor`, and each tail is the probability of the translation.
## LoD and shape changes during decoding
-According the image above, the only phrase to change LoD is beam search.
+According to the image above, the only phase that changes the LoD is beam search.
## Beam search design
-The beam search algorthm will be implemented as one method of the sequence decoder, it has 3 inputs
+The beam search algorithm will be implemented as one method of the sequence decoder and has 3 inputs:
-1. `topk_ids`, top K candidate ids for each prefix.
+1. `topk_ids`, the top K candidate ids for each prefix.
2. `topk_scores`, the corresponding scores for `topk_ids`
3. `generated_scores`, the score of the prefixes.
-All of the are LoDTensors, so that the sequence affilication is clear.
-Beam search will keep a beam for each prefix and select a smaller candidate set for each prefix.
+All of these are LoDTensors, so that the sequence affiliation is clear. Beam search will keep a beam for each prefix and select a smaller candidate set for each prefix.
-It will return three variables
+It will return three variables:
1. `selected_ids`, the final candidate beam search function selected for the next step.
2. `selected_scores`, the scores for the candidates.
-3. `generated_scores`, the updated scores for each prefixes (with the new candidates appended).
+3. `generated_scores`, the updated scores for each prefix (with the new candidates appended).
## Introducing the LoD-based `Pack` and `Unpack` methods in `TensorArray`
-The `selected_ids`, `selected_scores` and `generated_scores` are LoDTensors,
-and they exist in each time step,
+The `selected_ids`, `selected_scores` and `generated_scores` are LoDTensors that exist at each time step,
so it is natural to store them in arrays.
-Currently, PaddlePaddle has a module called `TensorArray` which can store an array of tensors,
-the results of beam search are better to store in a `TensorArray`.
+Currently, PaddlePaddle has a module called `TensorArray` which can store an array of tensors. It is better to store the results of beam search in a `TensorArray`.
-The `Pack` and `UnPack` in `TensorArray` are used to package tensors in the array to a `LoDTensor` or split the `LoDTensor` to an array of tensors.
-It needs some extensions to support pack or unpack an array of `LoDTensors`.
+The `Pack` and `UnPack` in `TensorArray` are used to pack tensors in the array to an `LoDTensor` or split the `LoDTensor` to an array of tensors.
+It needs some extensions to support the packing or unpacking an array of `LoDTensors`.
diff --git a/paddle/capi/Main.cpp b/paddle/capi/Main.cpp
index 78c43949dfe325d0e1a6ba10ae51cb7b858f6c52..bb8249a5511c089ec2f2263ff4cc290f0a5a8fce 100644
--- a/paddle/capi/Main.cpp
+++ b/paddle/capi/Main.cpp
@@ -29,6 +29,9 @@ static void initPaddle(int argc, char** argv) {
extern "C" {
paddle_error paddle_init(int argc, char** argv) {
+ static bool isInit = false;
+ if (isInit) return kPD_NO_ERROR;
+
std::vector realArgv;
realArgv.reserve(argc + 1);
realArgv.push_back(strdup(""));
@@ -37,6 +40,7 @@ paddle_error paddle_init(int argc, char** argv) {
}
initPaddle(argc + 1, realArgv.data());
free(realArgv[0]);
+ isInit = true;
return kPD_NO_ERROR;
}
}
diff --git a/paddle/capi/Matrix.cpp b/paddle/capi/Matrix.cpp
index 53a36f8f20d1143470928f57eda6f575d9048236..d5b55e1c95f248f551e6a0a3b39123169dd7784f 100644
--- a/paddle/capi/Matrix.cpp
+++ b/paddle/capi/Matrix.cpp
@@ -121,6 +121,7 @@ paddle_error paddle_matrix_get_shape(paddle_matrix mat,
paddle_matrix paddle_matrix_create_sparse(
uint64_t height, uint64_t width, uint64_t nnz, bool isBinary, bool useGpu) {
+#ifndef PADDLE_MOBILE_INFERENCE
auto ptr = new paddle::capi::CMatrix();
ptr->mat = paddle::Matrix::createSparseMatrix(
height,
@@ -131,6 +132,9 @@ paddle_matrix paddle_matrix_create_sparse(
false,
useGpu);
return ptr;
+#else
+ return nullptr;
+#endif
}
paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
@@ -140,6 +144,7 @@ paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
uint64_t colSize,
float* valueArray,
uint64_t valueSize) {
+#ifndef PADDLE_MOBILE_INFERENCE
if (mat == nullptr) return kPD_NULLPTR;
auto ptr = cast(mat);
if (rowArray == nullptr || colArray == nullptr ||
@@ -160,4 +165,7 @@ paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
} else {
return kPD_NOT_SUPPORTED;
}
+#else
+ return kPD_NOT_SUPPORTED;
+#endif
}
diff --git a/paddle/capi/matrix.h b/paddle/capi/matrix.h
index bb5223f8a275fa2550bf8b7e94a9c4333de4c8c9..01b8bad2ee9f528f8622346f43b9ff82225a7e73 100644
--- a/paddle/capi/matrix.h
+++ b/paddle/capi/matrix.h
@@ -48,6 +48,7 @@ PD_API paddle_matrix paddle_matrix_create(uint64_t height,
* @param isBinary is binary (either 1 or 0 in matrix) or not.
* @param useGpu is using GPU or not.
* @return paddle_matrix.
+ * @note Mobile inference does not support this interface.
*/
PD_API paddle_matrix paddle_matrix_create_sparse(
uint64_t height, uint64_t width, uint64_t nnz, bool isBinary, bool useGpu);
@@ -129,6 +130,7 @@ PD_API paddle_error paddle_matrix_get_shape(paddle_matrix mat,
* NULL if the matrix is binary.
* @param [in] valueSize length of value array. Zero if the matrix is binary.
* @return paddle_error
+ * @note Mobile inference does not support this interface.
*/
PD_API paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
int* rowArray,
diff --git a/paddle/cuda/CMakeLists.txt b/paddle/cuda/CMakeLists.txt
index 0865b02c4f275f3d5069109917b05dff1393fc1e..efd1b7a73e1655f95eb83a5e2f59e82cbf7eba16 100755
--- a/paddle/cuda/CMakeLists.txt
+++ b/paddle/cuda/CMakeLists.txt
@@ -27,7 +27,9 @@ if(WITH_GPU)
set_source_files_properties(${CUDA_CXX_SOURCES}
PROPERTIES COMPILE_FLAGS "-D__NVCC__")
else()
+ if (NOT MOBILE_INFERENCE)
set(CUDA_CXX_SOURCES src/hl_warpctc_wrap.cc)
+ endif()
endif()
set(CUDA_CU_SOURCES
diff --git a/paddle/cuda/include/hl_cnn.h b/paddle/cuda/include/hl_cnn.h
index 6b56d9ec8d3daae96aaaa04ed79cb637331e2281..89c1f48edacbe0a4432957fe066481412db7e6e1 100644
--- a/paddle/cuda/include/hl_cnn.h
+++ b/paddle/cuda/include/hl_cnn.h
@@ -18,7 +18,7 @@ limitations under the License. */
#include "hl_base.h"
/**
- * @brief Maximum pool forward.
+ * @brief Maximum pool forward with Mask output.
*
* @param[in] frameCnt batch size of input image.
* @param[in] inputData input data.
@@ -35,7 +35,7 @@ limitations under the License. */
* @param[in] paddingW padding width.
* @param[out] tgtData output data.
* @param[in] tgtStride stride between output data samples.
- *
+ * @param[out] maskData the location indices of select max data.
*/
extern void hl_maxpool_forward(const int frameCnt,
const real* inputData,
@@ -51,7 +51,8 @@ extern void hl_maxpool_forward(const int frameCnt,
const int paddingH,
const int paddingW,
real* tgtData,
- const int tgtStride);
+ const int tgtStride,
+ real* maskData = NULL);
/**
* @brief Maximum pool backward.
diff --git a/paddle/cuda/include/stub/hl_cnn_stub.h b/paddle/cuda/include/stub/hl_cnn_stub.h
index a76dbf0b6578de0606702ad1af227fbf6e1cd62e..968ed4840ffb0623b57bd6e6d839973e109394de 100644
--- a/paddle/cuda/include/stub/hl_cnn_stub.h
+++ b/paddle/cuda/include/stub/hl_cnn_stub.h
@@ -31,7 +31,8 @@ inline void hl_maxpool_forward(const int frameCnt,
const int paddingH,
const int paddingW,
real* tgtData,
- const int tgtStride) {}
+ const int tgtStride,
+ real* MaskData) {}
inline void hl_maxpool_backward(const int frameCnt,
const real* inputData,
diff --git a/paddle/cuda/src/hl_cuda_cnn.cu b/paddle/cuda/src/hl_cuda_cnn.cu
index 58674febdc4a094c95ff03701e4586c32729847d..3699b1e8ae9d8f813439eaeaa760c4a9f6e100a0 100644
--- a/paddle/cuda/src/hl_cuda_cnn.cu
+++ b/paddle/cuda/src/hl_cuda_cnn.cu
@@ -31,7 +31,8 @@ __global__ void KeMaxPoolForward(const int nthreads,
const int offsetH,
const int offsetW,
real* tgtData,
- const int tgtStride) {
+ const int tgtStride,
+ real* maskData) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < nthreads) {
int pw = index % pooledW;
@@ -45,16 +46,22 @@ __global__ void KeMaxPoolForward(const int nthreads,
hstart = max(hstart, 0);
wstart = max(wstart, 0);
real maxval = -FLT_MAX;
+ int max_index = -1;
inputData += (frameNum * channels + c) * height * width;
for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
- if (maxval < inputData[h * width + w])
- maxval = inputData[h * width + w];
+ if (maxval < inputData[h * width + w]) {
+ max_index = h * width + w;
+ maxval = inputData[max_index];
+ }
}
}
int tgtIndex =
index % (pooledW * pooledH * channels) + frameNum * tgtStride;
tgtData[tgtIndex] = maxval;
+ if (maskData != NULL) {
+ maskData[tgtIndex] = max_index;
+ }
}
}
@@ -72,7 +79,8 @@ void hl_maxpool_forward(const int frameCnt,
const int paddingH,
const int paddingW,
real* tgtData,
- const int tgtStride) {
+ const int tgtStride,
+ real* maskData) {
int num_kernels = pooledH * pooledW * channels * frameCnt;
int blocks = (num_kernels + 1024 - 1) / 1024;
dim3 threads(1024, 1);
@@ -92,7 +100,8 @@ void hl_maxpool_forward(const int frameCnt,
paddingH,
paddingW,
tgtData,
- tgtStride);
+ tgtStride,
+ maskData);
CHECK_SYNC("hl_maxpool_forward failed");
}
diff --git a/paddle/framework/CMakeLists.txt b/paddle/framework/CMakeLists.txt
index 1afc5242081e7f7b12527a15d29421cebeb3d3b8..c08e844847737b1172f6453767cc7f5e7b1a2bda 100644
--- a/paddle/framework/CMakeLists.txt
+++ b/paddle/framework/CMakeLists.txt
@@ -38,9 +38,9 @@ py_proto_compile(framework_py_proto SRCS framework.proto)
add_custom_target(framework_py_proto_init ALL COMMAND ${CMAKE_COMMAND} -E touch __init__.py)
add_dependencies(framework_py_proto framework_py_proto_init)
add_custom_command(TARGET framework_py_proto POST_BUILD
- COMMAND ${CMAKE_COMMAND} -E make_directory ${PADDLE_SOURCE_DIR}/python/paddle/v2/framework/proto
- COMMAND cp *.py ${PADDLE_SOURCE_DIR}/python/paddle/v2/framework/proto/
- COMMENT "Copy generated python proto into directory paddle/v2/framework/proto."
+ COMMAND ${CMAKE_COMMAND} -E make_directory ${PADDLE_SOURCE_DIR}/python/paddle/v2/fluid/proto
+ COMMAND cp *.py ${PADDLE_SOURCE_DIR}/python/paddle/v2/fluid/proto/
+ COMMENT "Copy generated python proto into directory paddle/v2/fluid/proto."
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
cc_library(backward SRCS backward.cc DEPS net_op)
diff --git a/paddle/framework/backward.cc b/paddle/framework/backward.cc
index 913cd0f81eaef37014f38c71e7c3d23bfeec1466..00d9dd238ec5328be28f58f8118daad3a039e08c 100644
--- a/paddle/framework/backward.cc
+++ b/paddle/framework/backward.cc
@@ -270,6 +270,19 @@ static bool AllGradInSet(const std::vector& names,
return false;
}
}
+ if (VLOG_IS_ON(10)) {
+ std::ostringstream sout;
+ sout << "All input {";
+ for (auto& name : names) {
+ sout << name << ",";
+ }
+ sout << "} is in {";
+ for (auto& name : set) {
+ sout << name << ",";
+ }
+ sout << "}";
+ VLOG(10) << sout.str();
+ }
return true;
}
@@ -290,14 +303,12 @@ static void CreateGradVarInBlock(
auto ops = block_desc->AllOps();
for (size_t op_index = grad_op_start_index; op_index < ops.size();
++op_index) {
- bool need_infer_shape = false;
std::unordered_set new_vars;
ForEachVarName(ops[op_index]->Outputs(),
[&](const std::string& grad_var_name) {
if (block_desc->HasVar(grad_var_name)) {
return false;
}
- need_infer_shape = true;
auto var = block_desc->Var(grad_var_name);
new_vars.insert(var->Name());
auto it = param_name_map.find(grad_var_name);
@@ -311,23 +322,21 @@ static void CreateGradVarInBlock(
grad_record.op_idx_ = static_cast(op_index);
return false; /* not break */
});
- if (need_infer_shape) {
- ops[op_index]->InferVarType(block_desc);
- for (auto& arg : ops[op_index]->OutputArgumentNames()) {
- if (new_vars.find(arg) == new_vars.end()) {
- continue;
- }
- auto pname = FwdName(arg);
- auto* param = block_desc->FindVarRecursive(pname);
- auto* grad = block_desc->FindVar(arg);
- if (param == nullptr) {
- grad->SetDataType(DataType::FP32);
- } else {
- grad->SetDataType(param->GetDataType());
- }
+ ops[op_index]->InferVarType(block_desc);
+ for (auto& arg : ops[op_index]->OutputArgumentNames()) {
+ if (new_vars.find(arg) == new_vars.end()) {
+ continue;
+ }
+ auto pname = FwdName(arg);
+ auto* param = block_desc->FindVarRecursive(pname);
+ auto* grad = block_desc->FindVar(arg);
+ if (param == nullptr) {
+ grad->SetDataType(DataType::FP32);
+ } else {
+ grad->SetDataType(param->GetDataType());
}
- ops[op_index]->InferShape(*block_desc);
}
+ ops[op_index]->InferShape(*block_desc);
}
}
@@ -377,10 +386,17 @@ std::vector> MakeOpGrad(
return grad_op_descs;
}
+static BlockDescBind* CreateStepBlock(
+ ProgramDescBind& program_desc,
+ std::unordered_set* no_grad_vars,
+ std::unordered_map* grad_to_var,
+ int step_block_idx);
+
std::vector> MakeBlockBackward(
ProgramDescBind& program_desc, int block_idx,
std::unordered_set* no_grad_vars,
std::unordered_map* grad_to_var) {
+ VLOG(5) << "MakeBlockBackward";
BlockDescBind* cur_block = program_desc.MutableBlock(block_idx);
std::vector op_descs = cur_block->AllOps();
std::unordered_map> dup_out_ops;
@@ -388,22 +404,32 @@ std::vector> MakeBlockBackward(
std::vector> backward_descs;
for (auto it = op_descs.rbegin(); it != op_descs.rend(); ++it) {
+ VLOG(5) << "Making backward " << (*it)->Type() << " op";
std::vector> op_grads;
- if ((*it)->Type() == "recurrent") {
+ if ((*it)->Type() == "recurrent" || (*it)->Type() == "while") {
int step_block_idx = (*it)->GetBlockAttr("step_block");
- auto backward_block_op_descs = MakeBlockBackward(
- program_desc, step_block_idx, no_grad_vars, grad_to_var);
+ BlockDescBind* backward_block = CreateStepBlock(
+ program_desc, no_grad_vars, grad_to_var, step_block_idx);
+ op_grads = MakeOpGrad(*it, no_grad_vars, grad_to_var, {backward_block});
+ } else if ((*it)->Type() == "conditional_block") {
BlockDescBind* backward_block =
- program_desc.AppendBlock(*program_desc.MutableBlock(step_block_idx));
- for (auto& ptr : backward_block_op_descs) {
- backward_block->AppendAllocatedOp(std::move(ptr));
- }
+ CreateStepBlock(program_desc, no_grad_vars, grad_to_var,
+ (*it)->GetBlockAttr("block"));
op_grads = MakeOpGrad(*it, no_grad_vars, grad_to_var, {backward_block});
} else {
op_grads = MakeOpGrad(*it, no_grad_vars, grad_to_var);
}
+ if (VLOG_IS_ON(10)) {
+ std::ostringstream sout;
+ sout << "Made ";
+ for (auto& op_grad : op_grads) {
+ sout << op_grad->Type() << " ";
+ }
+ VLOG(10) << sout.str();
+ }
+
for (const auto& desc : op_grads) {
for (const std::string& out_name : desc->OutputArgumentNames()) {
if (out_name.find("@GRAD") == std::string::npos) {
@@ -419,6 +445,8 @@ std::vector> MakeBlockBackward(
op_grads.begin(), op_grads.end(), std::back_inserter(backward_descs),
[](std::unique_ptr& ptr) { return std::move(ptr); });
}
+
+ VLOG(5) << "Appending Sums";
// Check whether some variables are written more than once
std::list>> pending_sum_ops;
for (const auto& dup : dup_out_ops) {
@@ -426,16 +454,22 @@ std::vector> MakeBlockBackward(
const std::vector dup_op = dup.second;
if (out_name != kEmptyVarName && dup_op.size() > 1) {
std::vector sum_op_inputs;
+ std::string next_g_name = out_name;
for (size_t i = 0; i < dup_op.size(); ++i) {
+ VLOG(10) << backward_descs[dup_op[i]]->Type() << " has " << out_name
+ << " duplicated";
std::string new_name = out_name + "@RENAME@" + std::to_string(i);
- backward_descs[dup_op[i]]->Rename(out_name, new_name);
+ backward_descs[dup_op[i]]->RenameOutput(out_name, new_name);
+ backward_descs[dup_op[i]]->RenameInput(out_name, next_g_name);
sum_op_inputs.emplace_back(new_name);
+ next_g_name = sum_op_inputs.back();
}
std::unique_ptr sum_op(new OpDescBind(
"sum", {{"X", sum_op_inputs}}, {{"Out", {out_name}}}, {}));
pending_sum_ops.push_back({dup_op.back(), std::move(sum_op)});
}
}
+
pending_sum_ops.sort(
[](const std::pair>& a,
const std::pair>& b) {
@@ -446,9 +480,26 @@ std::vector> MakeBlockBackward(
std::move(p.second));
}
+ VLOG(5) << "MakeBlockBackward Finished";
+
return backward_descs;
}
+static BlockDescBind* CreateStepBlock(
+ ProgramDescBind& program_desc,
+ std::unordered_set* no_grad_vars,
+ std::unordered_map* grad_to_var,
+ int step_block_idx) {
+ auto backward_block_op_descs = MakeBlockBackward(program_desc, step_block_idx,
+ no_grad_vars, grad_to_var);
+ BlockDescBind* backward_block =
+ program_desc.AppendBlock(*program_desc.MutableBlock(step_block_idx));
+ for (auto& ptr : backward_block_op_descs) {
+ backward_block->AppendAllocatedOp(move(ptr));
+ }
+ return backward_block;
+}
+
ParamGradInfoMap AppendBackward(
ProgramDescBind& program_desc, const VarDescBind& target,
const std::unordered_set& no_grad_vars) {
diff --git a/paddle/framework/data_type.h b/paddle/framework/data_type.h
index 3ec88d7a72c3339bf5e7d0ca3957a3f608f039b7..be144d8fc0104fccc08006532a85906ade25c2a1 100644
--- a/paddle/framework/data_type.h
+++ b/paddle/framework/data_type.h
@@ -29,6 +29,8 @@ inline DataType ToDataType(std::type_index type) {
return DataType::INT32;
} else if (typeid(int64_t).hash_code() == type.hash_code()) {
return DataType::INT64;
+ } else if (typeid(bool).hash_code() == type.hash_code()) {
+ return DataType::BOOL;
} else {
PADDLE_THROW("Not supported");
}
diff --git a/paddle/framework/ddim.cc b/paddle/framework/ddim.cc
index 53b899a23997b71e723a298ec360a4e018d89878..8b6f42b82df14bfcd25f33ef16b5903fb965a8ba 100644
--- a/paddle/framework/ddim.cc
+++ b/paddle/framework/ddim.cc
@@ -60,8 +60,7 @@ void make_ddim(DDim& ddim, const int64_t* dims, int n) {
ddim = make_dim<9>(dims);
break;
default:
- throw std::invalid_argument(
- "Dynamic dimensions must have between [1, 9] dimensions.");
+ PADDLE_THROW("Dynamic dimensions must have between [1, 9] dimensions.");
}
}
diff --git a/paddle/framework/executor.cc b/paddle/framework/executor.cc
index 2fcf41d69f0011b0d9a3d89c97fcebacb0703e97..adedd8cb0e8504fd6fc924e62a2ede3c1c7ce698 100644
--- a/paddle/framework/executor.cc
+++ b/paddle/framework/executor.cc
@@ -120,6 +120,7 @@ void Executor::Run(const ProgramDescBind& pdesc, Scope* scope, int block_id,
for (auto& op_desc : block.AllOps()) {
auto op = paddle::framework::OpRegistry::CreateOp(*op_desc);
+ VLOG(10) << op->DebugString();
op->Run(*local_scope, *device);
}
if (create_local_scope) {
diff --git a/paddle/framework/op_desc.cc b/paddle/framework/op_desc.cc
index 39c8def82e1ebb10a0e357a648af760099020c32..48cd131550dea5ad3f368b25c31d753efbe0dff9 100644
--- a/paddle/framework/op_desc.cc
+++ b/paddle/framework/op_desc.cc
@@ -235,6 +235,23 @@ void OpDescBind::Rename(const std::string &old_name,
need_update_ = true;
}
+void OpDescBind::RenameOutput(const std::string &old_name,
+ const std::string &new_name) {
+ for (auto &output : outputs_) {
+ std::replace(output.second.begin(), output.second.end(), old_name,
+ new_name);
+ }
+ need_update_ = true;
+}
+
+void OpDescBind::RenameInput(const std::string &old_name,
+ const std::string &new_name) {
+ for (auto &input : inputs_) {
+ std::replace(input.second.begin(), input.second.end(), old_name, new_name);
+ }
+ need_update_ = true;
+}
+
struct SetAttrDescVisitor : public boost::static_visitor {
explicit SetAttrDescVisitor(OpDesc::Attr *attr) : attr_(attr) {}
mutable OpDesc::Attr *attr_;
@@ -448,7 +465,12 @@ const std::vector &CompileTimeInferShapeContext::Outputs(
DDim CompileTimeInferShapeContext::GetDim(const std::string &name) const {
auto var = block_.FindVarRecursive(name);
PADDLE_ENFORCE(var != nullptr, "Cannot find variable %s", name);
- return framework::make_ddim(var->Shape());
+ try {
+ return framework::make_ddim(var->Shape());
+ } catch (...) {
+ VLOG(5) << "GetDim of variable " << name << " error";
+ std::rethrow_exception(std::current_exception());
+ }
}
void CompileTimeInferShapeContext::SetDim(const std::string &name,
diff --git a/paddle/framework/op_desc.h b/paddle/framework/op_desc.h
index e3e96441bbf51729f2ba69c9257e6961b1de0d5c..da032319afa775571d3942bf6ae415db7d233735 100644
--- a/paddle/framework/op_desc.h
+++ b/paddle/framework/op_desc.h
@@ -73,6 +73,10 @@ class OpDescBind {
void Rename(const std::string &old_name, const std::string &new_name);
+ void RenameOutput(const std::string &old_name, const std::string &new_name);
+
+ void RenameInput(const std::string &old_name, const std::string &new_name);
+
// Only be used in C++
const AttributeMap &GetAttrMap() const;
diff --git a/paddle/framework/operator.cc b/paddle/framework/operator.cc
index 3276f8af396fe58450a8dc6713fe61e49d5ca708..93467ab8ac796277b47a861a427de2837fb2d3d4 100644
--- a/paddle/framework/operator.cc
+++ b/paddle/framework/operator.cc
@@ -403,19 +403,6 @@ class RuntimeInferShapeContext : public InferShapeContext {
void OperatorWithKernel::Run(const Scope& scope,
const platform::DeviceContext& dev_ctx) const {
- if (VLOG_IS_ON(1)) {
- auto inputs = this->InputVars();
- auto outputs = this->OutputVars(true);
- std::ostringstream sout;
- sout << "Run operator " << this->Type() << " From [";
- std::ostream_iterator out_it(sout, ",");
- std::copy(inputs.begin(), inputs.end(), out_it);
- sout << "] to [";
- std::copy(outputs.begin(), outputs.end(), out_it);
- sout << "]";
- VLOG(1) << sout.str();
- }
-
RuntimeInferShapeContext infer_shape_ctx(*this, scope);
this->InferShape(&infer_shape_ctx);
diff --git a/paddle/framework/scope.cc b/paddle/framework/scope.cc
index 9428b8a07ea0af005f6e960ddaa02da624ad9d97..9ad6272c99dd6a85520ae44c1331ac232bc6a9a2 100644
--- a/paddle/framework/scope.cc
+++ b/paddle/framework/scope.cc
@@ -38,11 +38,12 @@ Scope& Scope::NewScope() const {
Variable* Scope::Var(const std::string& name) {
auto iter = vars_.find(name);
if (iter != vars_.end()) {
+ VLOG(3) << "Get existing variable " << name;
return iter->second;
}
Variable* v = new Variable();
vars_[name] = v;
- VLOG(3) << "Create variable " << name << " on scope";
+ VLOG(3) << "Create variable " << name;
v->name_ = &(vars_.find(name)->first);
return v;
}
diff --git a/paddle/framework/shape_inference.h b/paddle/framework/shape_inference.h
index 7d36ead2ca85328c7843b3b5d423cf8e921d1c93..05dc47f06ac81f0acb6d0317cbecb3009c7dd7f0 100644
--- a/paddle/framework/shape_inference.h
+++ b/paddle/framework/shape_inference.h
@@ -53,6 +53,10 @@ class InferShapeContext {
virtual bool IsRuntime() const = 0;
+ // Note: In while op, we need this to be public
+ void SetDims(const std::vector &names,
+ const std::vector &dims);
+
protected:
virtual framework::DDim GetDim(const std::string &name) const = 0;
virtual void SetDim(const std::string &name, const framework::DDim &dim) = 0;
@@ -60,9 +64,6 @@ class InferShapeContext {
std::vector GetDims(
const std::vector &names) const;
- void SetDims(const std::vector &names,
- const std::vector &dims);
-
std::vector GetVarTypes(
const std::vector &names) const;
diff --git a/paddle/function/ConvOp.h b/paddle/function/ConvOp.h
index baf78bc6c88d0d294f4457b81c52b22e425d9fdb..062ea25a11470dd9ecdafb278dee9a2e0979f00b 100644
--- a/paddle/function/ConvOp.h
+++ b/paddle/function/ConvOp.h
@@ -61,6 +61,7 @@ public:
// function arguments
strides_ = config.get>("strides");
paddings_ = config.get>("paddings");
+ dilations_ = config.get>("dilations");
groups_ = config.get("groups");
// number of inputs and outputs
@@ -118,6 +119,7 @@ protected:
std::vector strides_;
std::vector paddings_;
+ std::vector dilations_;
/// Group size, refer to grouped convolution in
/// Alex Krizhevsky's paper: when group=2, the first half of the
@@ -133,6 +135,10 @@ protected:
inline int paddingW() const { return paddings_[1]; }
+ inline int dilationH() const { return dilations_[0]; }
+
+ inline int dilationW() const { return dilations_[1]; }
+
// A temporary memory in convolution calculation.
MemoryHandlePtr memory_;
diff --git a/paddle/function/ConvOpTest.h b/paddle/function/ConvOpTest.h
index cb02a96d0dbef6f64fd9e7576179572e68bf5513..d8d3c792df236ab0fd412b0cf77f275355848627 100644
--- a/paddle/function/ConvOpTest.h
+++ b/paddle/function/ConvOpTest.h
@@ -79,45 +79,59 @@ void Convolution(const std::string& conv1,
if (outputChannels < inputChannels) continue;
for (size_t stride : {1, 2}) {
for (size_t padding : {0, 1}) {
- if (padding >= filterSize) break;
+ for (size_t dilation : {1, 3}) {
+ if (padding >= filterSize) break;
+ size_t filterS = (filterSize - 1) * dilation + 1;
- // NNPACK only supports stride = 1 if batchSize > 1
- if ((conv1 == "NNPACKConv-CPU" || conv2 == "NNPACKConv-CPU") &&
- batchSize > 1 && stride > 1)
- break;
+ if (inputSize + 2 * padding < filterS) break;
- size_t outputSize =
- (inputSize - filterSize + 2 * padding + stride) / stride;
- VLOG(3) << " batchSize=" << batchSize
- << " inputChannels=" << inputChannels
- << " inputHeight=" << inputSize
- << " inputWidth=" << inputSize
- << " outputChannels=" << outputChannels
- << " filterHeight=" << filterSize
- << " filterWidth=" << filterSize
- << " outputHeight=" << outputSize
- << " outputWidth=" << outputSize << " stride=" << stride
- << " padding=" << padding;
+ if ((conv1 == "NaiveConv-CPU" || conv2 == "NaiveConv-CPU" ||
+ conv1 == "NNPACKConv-CPU" ||
+ conv2 == "NNPACKConv-CPU") &&
+ dilation > 1)
+ break;
- std::vector paddings = {padding, padding};
- std::vector strides = {stride, stride};
- Compare2Function test(
- conv1,
- conv2,
- FuncConfig()
- .set("paddings", paddings)
- .set("strides", strides)
- .set("groups", (size_t)1)
- .set("algo", (std::string) "auto"));
+ // NNPACK only supports stride = 1 if batchSize > 1
+ if ((conv1 == "NNPACKConv-CPU" ||
+ conv2 == "NNPACKConv-CPU") &&
+ batchSize > 1 && stride > 1)
+ break;
- TensorShape input{
- batchSize, inputChannels, inputSize, inputSize};
- TensorShape filter{
- outputChannels, inputChannels, filterSize, filterSize};
- TensorShape output{
- batchSize, outputChannels, outputSize, outputSize};
+ size_t outputSize =
+ (inputSize - filterS + 2 * padding + stride) / stride;
+ VLOG(3) << " batchSize=" << batchSize
+ << " inputChannels=" << inputChannels
+ << " inputHeight=" << inputSize
+ << " inputWidth=" << inputSize
+ << " outputChannels=" << outputChannels
+ << " filterHeight=" << filterSize
+ << " filterWidth=" << filterSize
+ << " outputHeight=" << outputSize
+ << " outputWidth=" << outputSize
+ << " stride=" << stride << " padding=" << padding;
- function(test, input, filter, output);
+ std::vector paddings = {padding, padding};
+ std::vector strides = {stride, stride};
+ std::vector dilations = {dilation, dilation};
+ Compare2Function test(
+ conv1,
+ conv2,
+ FuncConfig()
+ .set("paddings", paddings)
+ .set("strides", strides)
+ .set("dilations", dilations)
+ .set("groups", (size_t)1)
+ .set("algo", (std::string) "auto"));
+
+ TensorShape input{
+ batchSize, inputChannels, inputSize, inputSize};
+ TensorShape filter{
+ outputChannels, inputChannels, filterSize, filterSize};
+ TensorShape output{
+ batchSize, outputChannels, outputSize, outputSize};
+
+ function(test, input, filter, output);
+ }
}
}
}
@@ -144,6 +158,7 @@ void Convolution2(const std::string& conv1,
for (size_t outputChannels : {7}) {
size_t stride = 1;
size_t padding = 0;
+ size_t dilation = 1;
size_t outputHeight =
(inputHeight - filterHeight + 2 * padding + stride) /
stride;
@@ -162,6 +177,7 @@ void Convolution2(const std::string& conv1,
std::vector paddings = {padding, padding};
std::vector strides = {stride, stride};
+ std::vector dilations = {dilation, dilation};
Compare2Function test(
conv1,
conv2,
@@ -169,6 +185,7 @@ void Convolution2(const std::string& conv1,
.set("paddings", paddings)
.set("strides", strides)
.set("groups", (size_t)1)
+ .set("dilations", dilations)
.set("algo", (std::string) "auto"));
TensorShape input{
@@ -223,6 +240,7 @@ void DepthwiseConvolution(const std::string& conv1,
std::vector paddings = {padding, padding};
std::vector strides = {stride, stride};
+ std::vector dilations = {1, 1};
size_t groups = inputChannels;
Compare2Function test(
conv1,
@@ -231,6 +249,7 @@ void DepthwiseConvolution(const std::string& conv1,
.set("paddings", paddings)
.set("strides", strides)
.set("groups", groups)
+ .set("dilations", dilations)
.set("algo", (std::string) "auto"));
TensorShape input{
diff --git a/paddle/function/GemmConvOp.cpp b/paddle/function/GemmConvOp.cpp
index bdb56ddac38b91d756fc6f31282f29c0489fd660..8d34eee886a6202691e5dec2ab62e7c5b0ac7fb1 100644
--- a/paddle/function/GemmConvOp.cpp
+++ b/paddle/function/GemmConvOp.cpp
@@ -100,7 +100,9 @@ public:
strideH(),
strideW(),
paddingH(),
- paddingW());
+ paddingW(),
+ dilationH(),
+ dilationW());
} else {
colData = inputData + g * inputOffset;
}
@@ -223,7 +225,9 @@ public:
strideH(),
strideW(),
paddingH(),
- paddingW());
+ paddingW(),
+ dilationH(),
+ dilationW());
}
}
inputGrad += inputChannels * inputHeight * inputWidth;
@@ -310,7 +314,9 @@ public:
strideH(),
strideW(),
paddingH(),
- paddingW());
+ paddingW(),
+ dilationH(),
+ dilationW());
} else {
colData = inputData + g * inputOffset;
}
diff --git a/paddle/function/Im2Col.h b/paddle/function/Im2Col.h
index 1e0cff436ff60d5a029e89657d00af2b0bf8b454..0c37fc972484bfbede01d23652e384071bf883af 100644
--- a/paddle/function/Im2Col.h
+++ b/paddle/function/Im2Col.h
@@ -78,7 +78,9 @@ public:
int strideHeight,
int strideWidth,
int paddingHeight,
- int paddingWidth);
+ int paddingWidth,
+ int dilationHeight = 1,
+ int dilationWidth = 1);
};
template
@@ -91,7 +93,9 @@ public:
int strideHeight,
int strideWidth,
int paddingHeight,
- int paddingWidth);
+ int paddingWidth,
+ int dilationHeight = 1,
+ int dilationWidth = 1);
};
} // namespace paddle
diff --git a/paddle/function/Im2ColOp.cpp b/paddle/function/Im2ColOp.cpp
index b7d1eb1eded7a7471fd5833a649916d3ee3e598e..f864d42f8075209c70ca2e16a70e4f2c9d58eef4 100644
--- a/paddle/function/Im2ColOp.cpp
+++ b/paddle/function/Im2ColOp.cpp
@@ -31,7 +31,9 @@ public:
int strideHeight,
int strideWidth,
int paddingHeight,
- int paddingWidth) {
+ int paddingWidth,
+ int dilationHeight,
+ int dilationWidth) {
int inputChannels = imShape[0];
int inputHeight = imShape[1];
int inputWidth = imShape[2];
@@ -47,8 +49,8 @@ public:
int c_im = c / filterWidth / filterHeight;
for (int h = 0; h < outputHeight; ++h) {
for (int w = 0; w < outputWidth; ++w) {
- int imRowIdx = h * strideHeight + hOffset;
- int imColIdx = w * strideWidth + wOffset;
+ int imRowIdx = h * strideHeight + hOffset * dilationHeight;
+ int imColIdx = w * strideWidth + wOffset * dilationWidth;
if ((imRowIdx - paddingHeight) < 0 ||
(imRowIdx - paddingHeight) >= inputHeight ||
(imColIdx - paddingWidth) < 0 ||
@@ -81,7 +83,9 @@ public:
int strideHeight,
int strideWidth,
int paddingHeight,
- int paddingWidth) {
+ int paddingWidth,
+ int dilationHeight,
+ int dilationWidth) {
int inputChannels = imShape[0];
int inputHeight = imShape[1];
int inputWidth = imShape[2];
@@ -97,8 +101,8 @@ public:
int c_im = c / filterWidth / filterHeight;
for (int h = 0; h < outputHeight; ++h) {
for (int w = 0; w < outputWidth; ++w) {
- int imRowIdx = h * strideHeight + hOffset;
- int imColIdx = w * strideWidth + wOffset;
+ int imRowIdx = h * strideHeight + hOffset * dilationHeight;
+ int imColIdx = w * strideWidth + wOffset * dilationWidth;
if ((imRowIdx - paddingHeight) >= 0 &&
(imRowIdx - paddingHeight) < inputHeight &&
(imColIdx - paddingWidth) >= 0 &&
@@ -134,7 +138,9 @@ public:
int strideHeight,
int strideWidth,
int paddingHeight,
- int paddingWidth) {
+ int paddingWidth,
+ int dilationHeight = 1,
+ int dilationWidth = 1) {
int inputChannels = imShape[0];
int inputHeight = imShape[1];
int inputWidth = imShape[2];
@@ -147,9 +153,10 @@ public:
for (int channel = 0; channel < inputChannels; ++channel) {
for (int filterH = 0; filterH < filterHeight; ++filterH) {
for (int filterW = 0; filterW < filterWidth; ++filterW) {
- int imRowOffset =
- outputH * strideHeight + filterH - paddingHeight;
- int imColOffset = outputW * strideWidth + filterW - paddingWidth;
+ int imRowOffset = outputH * strideHeight +
+ filterH * dilationHeight - paddingHeight;
+ int imColOffset = outputW * strideWidth +
+ filterW * dilationWidth - paddingWidth;
int colDataOffset =
(((outputH * outputWidth + outputW) * inputChannels +
channel) *
@@ -189,7 +196,9 @@ public:
int strideHeight,
int strideWidth,
int paddingHeight,
- int paddingWidth) {
+ int paddingWidth,
+ int dilationHeight = 1,
+ int dilationWidth = 1) {
int inputChannels = imShape[0];
int inputHeight = imShape[1];
int inputWidth = imShape[2];
@@ -202,9 +211,10 @@ public:
for (int channel = 0; channel < inputChannels; ++channel) {
for (int filterH = 0; filterH < filterHeight; ++filterH) {
for (int filterW = 0; filterW < filterWidth; ++filterW) {
- int imRowOffset =
- outputH * strideHeight + filterH - paddingHeight;
- int imColOffset = outputW * strideWidth + filterW - paddingWidth;
+ int imRowOffset = outputH * strideHeight +
+ filterH * dilationHeight - paddingHeight;
+ int imColOffset = outputW * strideWidth +
+ filterW * dilationWidth - paddingWidth;
int colDataOffset =
(((outputH * outputWidth + outputW) * inputChannels +
channel) *
diff --git a/paddle/function/Im2ColOpGpu.cu b/paddle/function/Im2ColOpGpu.cu
index bd98610498b1af003574129118be4684d38e5813..71da11b95557d7b59de5ea6c65d1d43db42f211c 100644
--- a/paddle/function/Im2ColOpGpu.cu
+++ b/paddle/function/Im2ColOpGpu.cu
@@ -28,6 +28,8 @@ __global__ void im2col(const T* data_im,
int strideW,
int paddingH,
int paddingW,
+ int dilationH,
+ int dilationW,
int height_col,
int width_col,
T* data_col) {
@@ -44,8 +46,8 @@ __global__ void im2col(const T* data_im,
data_col += (channel_out * height_col + h_out) * width_col + w_out;
for (int i = 0; i < blockH; ++i) {
for (int j = 0; j < blockW; ++j) {
- int rIdx = int(h_in + i);
- int cIdx = int(w_in + j);
+ int rIdx = int(h_in + i * dilationH);
+ int cIdx = int(w_in + j * dilationW);
if ((rIdx - (int)paddingH) >= (int)height ||
(rIdx - (int)paddingH) < 0 ||
(cIdx - (int)paddingW) >= (int)width ||
@@ -77,7 +79,9 @@ public:
int strideHeight,
int strideWidth,
int paddingHeight,
- int paddingWidth) {
+ int paddingWidth,
+ int dilationHeight,
+ int dilationWidth) {
int inputChannels = imShape[0];
int inputHeight = imShape[1];
int inputWidth = imShape[2];
@@ -102,6 +106,8 @@ public:
strideWidth,
paddingHeight,
paddingWidth,
+ dilationHeight,
+ dilationWidth,
outputHeight,
outputWidth,
colData);
@@ -121,6 +127,8 @@ __global__ void col2im(size_t n,
size_t strideW,
size_t paddingH,
size_t paddingW,
+ size_t dilationH,
+ size_t dilationW,
size_t height_col,
size_t width_col,
T* data_im) {
@@ -131,23 +139,34 @@ __global__ void col2im(size_t n,
int w = int(index % width);
int h = int((index / width) % height);
int c = int(index / (width * height));
+ int filterH = (blockH - 1) * dilationH + 1;
+ int filterW = (blockW - 1) * dilationW + 1;
+
if ((w - (int)paddingW) >= 0 &&
(w - (int)paddingW) < (width - 2 * paddingW) &&
(h - (int)paddingH) >= 0 && (h - paddingH) < (height - 2 * paddingH)) {
// compute the start and end of the output
int w_col_start =
- (w < (int)blockW) ? 0 : (w - int(blockW)) / (int)strideW + 1;
+ (w < (int)filterW) ? 0 : (w - int(filterW)) / (int)strideW + 1;
int w_col_end = min((int)(w / (int)strideW + 1), (int)(width_col));
int h_col_start =
- (h < (int)blockH) ? 0 : (h - (int)blockH) / (int)strideH + 1;
+ (h < (int)filterH) ? 0 : (h - (int)filterH) / (int)strideH + 1;
int h_col_end = min(int(h / strideH + 1), int(height_col));
+
for (int h_col = h_col_start; h_col < h_col_end; ++h_col) {
for (int w_col = w_col_start; w_col < w_col_end; ++w_col) {
// the col location: [c * width * height + h_out, w_out]
- int c_col = int(c * blockH * blockW) +
- (h - h_col * (int)strideH) * (int)blockW +
- (w - w_col * (int)strideW);
- val += data_col[(c_col * height_col + h_col) * width_col + w_col];
+ int h_k = (h - h_col * strideH);
+ int w_k = (w - w_col * strideW);
+ if (h_k % dilationH == 0 && w_k % dilationW == 0) {
+ h_k /= dilationH;
+ w_k /= dilationW;
+ int c_col =
+ (((c * blockH + h_k) * blockW + w_k) * height_col + h_col) *
+ width_col +
+ w_col;
+ val += data_col[c_col];
+ }
}
}
h -= paddingH;
@@ -173,7 +192,9 @@ public:
int strideHeight,
int strideWidth,
int paddingHeight,
- int paddingWidth) {
+ int paddingWidth,
+ int dilationHeight,
+ int dilationWidth) {
int inputChannels = imShape[0];
int inputHeight = imShape[1];
int inputWidth = imShape[2];
@@ -205,6 +226,8 @@ public:
strideWidth,
paddingHeight,
paddingWidth,
+ dilationHeight,
+ dilationWidth,
outputHeight,
outputWidth,
imData);
@@ -229,6 +252,8 @@ __global__ void im2colOCF(const T* imData,
int strideWidth,
int paddingHeight,
int paddingWidth,
+ int dilationHeight,
+ int dilationWidth,
int outputHeight,
int outputWidth) {
int swId = blockIdx.x;
@@ -237,8 +262,10 @@ __global__ void im2colOCF(const T* imData,
channelId += blockDim.z) {
for (int idy = threadIdx.y; idy < filterHeight; idy += blockDim.y) {
for (int idx = threadIdx.x; idx < filterWidth; idx += blockDim.x) {
- int widthOffset = idx + swId * strideWidth - paddingWidth;
- int heightOffset = idy + shId * strideHeight - paddingHeight;
+ int widthOffset =
+ idx * dilationHeight + swId * strideWidth - paddingWidth;
+ int heightOffset =
+ idy * dilationWidth + shId * strideHeight - paddingHeight;
int imOffset = widthOffset + heightOffset * inputWidth +
channelId * inputHeight * inputWidth;
@@ -273,7 +300,9 @@ public:
int strideHeight,
int strideWidth,
int paddingHeight,
- int paddingWidth) {
+ int paddingWidth,
+ int dilationHeight,
+ int dilationWidth) {
int inputChannels = imShape[0];
int inputHeight = imShape[1];
int inputWidth = imShape[2];
@@ -312,6 +341,8 @@ public:
strideWidth,
paddingHeight,
paddingWidth,
+ dilationHeight,
+ dilationWidth,
outputHeight,
outputWidth);
CHECK_SYNC("Im2ColFunctor GPU failed");
@@ -330,6 +361,8 @@ __global__ void col2imOCF(T* imData,
int strideWidth,
int paddingHeight,
int paddingWidth,
+ int dilationHeight,
+ int dilationWidth,
int outputHeight,
int outputWidth) {
int swId = blockIdx.x;
@@ -338,8 +371,10 @@ __global__ void col2imOCF(T* imData,
channelId += blockDim.z) {
for (int idy = threadIdx.y; idy < filterHeight; idy += blockDim.y) {
for (int idx = threadIdx.x; idx < filterWidth; idx += blockDim.x) {
- int widthOffset = idx + swId * strideWidth - paddingWidth;
- int heightOffset = idy + shId * strideHeight - paddingHeight;
+ int widthOffset =
+ idx * dilationWidth + swId * strideWidth - paddingWidth;
+ int heightOffset =
+ idy * dilationHeight + shId * strideHeight - paddingHeight;
int imOffset = widthOffset + heightOffset * inputWidth +
channelId * inputHeight * inputWidth;
@@ -372,7 +407,9 @@ public:
int strideHeight,
int strideWidth,
int paddingHeight,
- int paddingWidth) {
+ int paddingWidth,
+ int dilationHeight,
+ int dilationWidth) {
int inputChannels = imShape[0];
int inputHeight = imShape[1];
int inputWidth = imShape[2];
@@ -411,6 +448,8 @@ public:
strideWidth,
paddingHeight,
paddingWidth,
+ dilationHeight,
+ dilationWidth,
outputHeight,
outputWidth);
CHECK_SYNC("Col2ImFunctor GPU failed");
diff --git a/paddle/function/Im2ColTest.cpp b/paddle/function/Im2ColTest.cpp
index a0a01a5fc7fc055dce6ddb3ee51c7ab18f8a4ca7..1f085538d81904dbd5b5d6bcd014adaed22e37d7 100644
--- a/paddle/function/Im2ColTest.cpp
+++ b/paddle/function/Im2ColTest.cpp
@@ -29,82 +29,98 @@ void TestIm2ColFunctor() {
for (size_t filterWidth : {3, 7}) {
for (size_t stride : {1, 2}) {
for (size_t padding : {0, 1}) {
- if (inputHeight <= filterHeight || inputWidth <= filterWidth)
- break;
- if (padding >= filterHeight || padding >= filterWidth) break;
- size_t outputHeight =
- (inputHeight - filterHeight + 2 * padding + stride) /
- stride;
- size_t outputWidth =
- (inputWidth - filterWidth + 2 * padding + stride) / stride;
-
- TensorShape imShape =
- TensorShape({channels, inputHeight, inputWidth});
- TensorShape colShape1 = TensorShape({channels,
- filterHeight,
- filterWidth,
- outputHeight,
- outputWidth});
- TensorShape colShape2 = TensorShape({outputHeight,
- outputWidth,
- channels,
- filterHeight,
- filterWidth});
-
- size_t height = channels * filterHeight * filterWidth;
- size_t width = outputHeight * outputWidth;
- VectorPtr input1 = Vector::create(imShape.getElements(), false);
- VectorPtr input2 = Vector::create(imShape.getElements(), false);
- MatrixPtr output1 = Matrix::create(height, width, false, false);
- MatrixPtr output2 = Matrix::create(width, height, false, false);
- input1->uniform(0.001, 1);
- input2->copyFrom(*input1);
-
- Im2ColFunctor im2Col1;
- Im2ColFunctor im2Col2;
- im2Col1(input1->getData(),
- imShape,
- output1->getData(),
- colShape1,
- stride,
- stride,
- padding,
- padding);
- im2Col2(input2->getData(),
- imShape,
- output2->getData(),
- colShape2,
- stride,
- stride,
- padding,
- padding);
-
- // The transposition of the result of ColFormat == kCFO
- // is equal to the result of ColFormat == kOCF.
- MatrixPtr test;
- output2->transpose(test, true);
- autotest::TensorCheckErr(*output1, *test);
-
- Col2ImFunctor col2Im1;
- Col2ImFunctor col2Im2;
- col2Im1(input1->getData(),
- imShape,
- output1->getData(),
- colShape1,
- stride,
- stride,
- padding,
- padding);
- col2Im2(input2->getData(),
- imShape,
- output2->getData(),
- colShape2,
- stride,
- stride,
- padding,
- padding);
-
- autotest::TensorCheckErr(*input1, *input2);
+ for (size_t dilation : {1, 3}) {
+ size_t filterSizeH = (filterHeight - 1) * dilation + 1;
+ size_t filterSizeW = (filterWidth - 1) * dilation + 1;
+ if (inputHeight + 2 * padding < filterSizeH ||
+ inputWidth + 2 * padding < filterSizeW)
+ break;
+ if (padding >= filterSizeH || padding >= filterSizeW) break;
+ size_t outputHeight =
+ (inputHeight - filterSizeH + 2 * padding) / stride + 1;
+ size_t outputWidth =
+ (inputWidth - filterSizeW + 2 * padding) / stride + 1;
+
+ TensorShape imShape =
+ TensorShape({channels, inputHeight, inputWidth});
+ TensorShape colShape1 = TensorShape({channels,
+ filterHeight,
+ filterWidth,
+ outputHeight,
+ outputWidth});
+ TensorShape colShape2 = TensorShape({outputHeight,
+ outputWidth,
+ channels,
+ filterHeight,
+ filterWidth});
+
+ size_t height = channels * filterHeight * filterWidth;
+ size_t width = outputHeight * outputWidth;
+ VectorPtr input1 =
+ Vector::create(imShape.getElements(), false);
+ VectorPtr input2 =
+ Vector::create(imShape.getElements(), false);
+ MatrixPtr output1 =
+ Matrix::create(height, width, false, false);
+ MatrixPtr output2 =
+ Matrix::create(width, height, false, false);
+ input1->uniform(0.001, 1);
+ input2->copyFrom(*input1);
+
+ Im2ColFunctor im2Col1;
+ Im2ColFunctor im2Col2;
+ im2Col1(input1->getData(),
+ imShape,
+ output1->getData(),
+ colShape1,
+ stride,
+ stride,
+ padding,
+ padding,
+ dilation,
+ dilation);
+ im2Col2(input2->getData(),
+ imShape,
+ output2->getData(),
+ colShape2,
+ stride,
+ stride,
+ padding,
+ padding,
+ dilation,
+ dilation);
+
+ // The transposition of the result of ColFormat == kCFO
+ // is equal to the result of ColFormat == kOCF.
+ MatrixPtr test;
+ output2->transpose(test, true);
+ autotest::TensorCheckErr(*output1, *test);
+
+ Col2ImFunctor col2Im1;
+ Col2ImFunctor col2Im2;
+
+ col2Im1(input1->getData(),
+ imShape,
+ output1->getData(),
+ colShape1,
+ stride,
+ stride,
+ padding,
+ padding,
+ dilation,
+ dilation);
+ col2Im2(input2->getData(),
+ imShape,
+ output2->getData(),
+ colShape2,
+ stride,
+ stride,
+ padding,
+ padding,
+ dilation,
+ dilation);
+ autotest::TensorCheckErr(*input1, *input2);
+ }
}
}
}
diff --git a/paddle/gserver/CMakeLists.txt b/paddle/gserver/CMakeLists.txt
index 5f39167afc34affbea7858fa0794ef52b786a383..91d732641a4a5eed050841b59fd10da397eb732f 100644
--- a/paddle/gserver/CMakeLists.txt
+++ b/paddle/gserver/CMakeLists.txt
@@ -85,9 +85,49 @@ if(MOBILE_INFERENCE)
gradientmachines/GradientMachineMode.cpp
gradientmachines/MultiGradientMachine.cpp)
- # Remove useless layers
+ # Remove layers that used in training
list(REMOVE_ITEM GSERVER_SOURCES
- layers/RecurrentLayerGroup.cpp)
+ layers/RecurrentLayerGroup.cpp
+ layers/CostLayer.cpp
+ layers/MultiBoxLossLayer.cpp
+ layers/WarpCTCLayer.cpp
+ layers/CTCLayer.cpp
+ layers/LinearChainCTC.cpp
+ layers/PrintLayer.cpp)
+ list(REMOVE_ITEM GSERVER_SOURCES
+ layers/OuterProdLayer.cpp
+ layers/SumToOneNormLayer.cpp
+ layers/ConvShiftLayer.cpp
+ layers/InterpolationLayer.cpp
+ layers/AgentLayer.cpp
+ layers/DotMulOperator.cpp
+ layers/GruStepLayer.cpp
+ layers/LstmStepLayer.cpp
+ layers/ConvexCombinationLayer.cpp
+ layers/Conv3DLayer.cpp
+ layers/DeConv3DLayer.cpp
+ layers/CropLayer.cpp
+ layers/CrossEntropyOverBeam.cpp
+ layers/DataNormLayer.cpp
+ layers/FeatureMapExpandLayer.cpp
+ layers/HierarchicalSigmoidLayer.cpp
+ layers/MultinomialSampler.cpp
+ layers/NCELayer.cpp
+ layers/KmaxSeqScoreLayer.cpp
+ layers/MDLstmLayer.cpp
+ layers/MultiplexLayer.cpp
+ layers/PadLayer.cpp
+ layers/Pool3DLayer.cpp
+ layers/ResizeLayer.cpp
+ layers/RotateLayer.cpp
+ layers/RowConvLayer.cpp
+ layers/RowL2NormLayer.cpp
+ layers/SamplingIdLayer.cpp
+ layers/ScaleShiftLayer.cpp
+ layers/SelectiveFullyConnectedLayer.cpp
+ layers/SpatialPyramidPoolLayer.cpp
+ layers/BilinearInterpLayer.cpp
+ layers/ClipLayer.cpp)
endif()
if(WITH_GPU)
diff --git a/paddle/gserver/gradientmachines/NeuralNetwork.cpp b/paddle/gserver/gradientmachines/NeuralNetwork.cpp
index dbadc352a4ccd7483bf67e1025c212f514e32a24..be112b41239cace3fa9b9ee97923f8c3c7a9a98f 100644
--- a/paddle/gserver/gradientmachines/NeuralNetwork.cpp
+++ b/paddle/gserver/gradientmachines/NeuralNetwork.cpp
@@ -16,7 +16,6 @@ limitations under the License. */
#include "NeuralNetwork.h"
#include "hl_gpu.h"
-#include "paddle/gserver/layers/AgentLayer.h"
#include "paddle/utils/CustomStackTrace.h"
#include "paddle/utils/Logging.h"
#include "paddle/utils/Stat.h"
@@ -28,6 +27,7 @@ limitations under the License. */
#ifndef PADDLE_MOBILE_INFERENCE
#include "MultiNetwork.h"
#include "RecurrentGradientMachine.h"
+#include "paddle/gserver/layers/AgentLayer.h"
#endif
namespace paddle {
@@ -192,9 +192,11 @@ void NeuralNetwork::init(const ModelConfig& config,
void NeuralNetwork::connect(LayerPtr agentLayer,
LayerPtr realLayer,
int height) {
+#ifndef PADDLE_MOBILE_INFERENCE
AgentLayer* agent = dynamic_cast(agentLayer.get());
CHECK_NOTNULL(agent);
agent->setRealLayer(realLayer, height);
+#endif
}
void NeuralNetwork::connect(std::string agentLayerName,
diff --git a/paddle/gserver/layers/ExpandConvLayer.cpp b/paddle/gserver/layers/ExpandConvLayer.cpp
index 48dfcb49a4c2c46891bb5236fc1f8e644c03f327..7ff0c73721d3de93aa7fa5fae58876884592c51f 100644
--- a/paddle/gserver/layers/ExpandConvLayer.cpp
+++ b/paddle/gserver/layers/ExpandConvLayer.cpp
@@ -79,6 +79,10 @@ bool ExpandConvLayer::init(const LayerMap &layerMap,
for (int i = 0; i < config_.inputs_size(); i++) {
std::vector paddings = {(size_t)paddingY_[i], (size_t)padding_[i]};
std::vector strides = {(size_t)strideY_[i], (size_t)stride_[i]};
+ std::vector dilations = {(size_t)dilationY_[i],
+ (size_t)dilation_[i]};
+
+ bool useDilation = ((size_t)dilationY_[i] > 1 || (size_t)dilation_[i] > 1);
// Convolution Layer uses the GemmConv function by default.
convType = "GemmConv";
@@ -97,13 +101,14 @@ bool ExpandConvLayer::init(const LayerMap &layerMap,
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
if ((filterSize_[i] == filterSizeY_[i]) &&
(filterSize_[i] == 3 || filterSize_[i] == 4) &&
- (stride_[i] == strideY_[i]) && (stride_[i] == 1 || stride_[i] == 2)) {
+ (stride_[i] == strideY_[i]) && (stride_[i] == 1 || stride_[i] == 2) &&
+ !useDilation) {
convType = "NeonDepthwiseConv";
}
#endif
}
- if (FLAGS_use_nnpack && !isDeconv_) {
+ if (FLAGS_use_nnpack && !isDeconv_ && !useDilation) {
createFunction(forward_,
"NNPACKConv",
FuncConfig()
@@ -117,6 +122,7 @@ bool ExpandConvLayer::init(const LayerMap &layerMap,
FuncConfig()
.set("paddings", paddings)
.set("strides", strides)
+ .set("dilations", dilations)
.set("groups", (size_t)groups_[i]));
createFunction(backward_,
@@ -124,6 +130,7 @@ bool ExpandConvLayer::init(const LayerMap &layerMap,
FuncConfig()
.set("paddings", paddings)
.set("strides", strides)
+ .set("dilations", dilations)
.set("groups", (size_t)groups_[i]));
createFunction(backward_,
@@ -131,6 +138,7 @@ bool ExpandConvLayer::init(const LayerMap &layerMap,
FuncConfig()
.set("paddings", paddings)
.set("strides", strides)
+ .set("dilations", dilations)
.set("groups", (size_t)groups_[i]));
}
}
diff --git a/paddle/gserver/layers/Layer.cpp b/paddle/gserver/layers/Layer.cpp
index 01f2aae6cf88d47296da804061b9b039cca593db..b55b86221cd411addfa8c5e93f8089f5ed9b0557 100644
--- a/paddle/gserver/layers/Layer.cpp
+++ b/paddle/gserver/layers/Layer.cpp
@@ -98,6 +98,7 @@ ClassRegistrar Layer::registrar_;
LayerPtr Layer::create(const LayerConfig& config) {
std::string type = config.type();
+#ifndef PADDLE_MOBILE_INFERENCE
// NOTE: As following types have illegal character '-',
// they can not use REGISTER_LAYER to registrar.
// Besides, to fit with old training models,
@@ -106,7 +107,6 @@ LayerPtr Layer::create(const LayerConfig& config) {
return LayerPtr(new MultiClassCrossEntropy(config));
else if (type == "rank-cost")
return LayerPtr(new RankingCost(config));
-#ifndef PADDLE_MOBILE_INFERENCE
else if (type == "auc-validation")
return LayerPtr(new AucValidation(config));
else if (type == "pnpair-validation")
diff --git a/paddle/gserver/layers/MaxPoolWithMaskLayer.cpp b/paddle/gserver/layers/MaxPoolWithMaskLayer.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..d810a58d9a3aea4333806dc9805d3444c3772ba3
--- /dev/null
+++ b/paddle/gserver/layers/MaxPoolWithMaskLayer.cpp
@@ -0,0 +1,109 @@
+/* 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 "MaxPoolWithMaskLayer.h"
+#include "paddle/utils/Logging.h"
+#include "paddle/utils/Stat.h"
+
+namespace paddle {
+
+bool MaxPoolWithMaskLayer::init(const LayerMap& layerMap,
+ const ParameterMap& parameterMap) {
+ PoolLayer::init(layerMap, parameterMap);
+ setOutput("mask", &mask_);
+ return true;
+}
+
+size_t MaxPoolWithMaskLayer::getSize() {
+ CHECK_EQ(inputLayers_.size(), 1UL);
+ size_t layerSize = 0;
+
+ outputY_ = outputSize(imgSizeY_,
+ sizeY_,
+ confPaddingY_,
+ strideY_,
+ /* caffeMode */ false);
+ outputX_ = outputSize(imgSize_,
+ sizeX_,
+ confPadding_,
+ stride_,
+ /* caffeMode */ false);
+
+ layerSize = outputX_ * outputY_ * channels_;
+ getOutput().setFrameHeight(outputY_);
+ getOutput().setFrameWidth(outputX_);
+
+ return layerSize;
+}
+
+void MaxPoolWithMaskLayer::forward(PassType passType) {
+ size_t size = getSize();
+ MatrixPtr inputV = inputLayers_[0]->getOutputValue();
+ int batchSize = inputV->getHeight();
+ resetOutput(batchSize, size);
+
+ MatrixPtr outV = getOutputValue();
+ CHECK_EQ(size, outV->getWidth());
+
+ resetSpecifyOutput(mask_,
+ batchSize,
+ size,
+ /* isValueClean */ false,
+ /* isGradClean */ true);
+
+ MatrixPtr maskV = mask_.value;
+ outV->maxPoolForward(*inputV,
+ imgSizeY_,
+ imgSize_,
+ channels_,
+ sizeX_,
+ sizeY_,
+ strideY_,
+ stride_,
+ outputY_,
+ outputX_,
+ confPaddingY_,
+ confPadding_,
+ maskV);
+}
+
+void MaxPoolWithMaskLayer::backward(const UpdateCallback& callback) {
+ (void)callback;
+ if (NULL == getInputGrad(0)) {
+ return;
+ }
+
+ MatrixPtr outGrad = getOutputGrad();
+ MatrixPtr inputV = inputLayers_[0]->getOutputValue();
+ MatrixPtr outV = getOutputValue();
+ MatrixPtr inputGrad = inputLayers_[0]->getOutputGrad();
+
+ inputGrad->maxPoolBackward(*inputV,
+ imgSizeY_,
+ imgSize_,
+ *outGrad,
+ *outV,
+ sizeX_,
+ sizeY_,
+ strideY_,
+ stride_,
+ outputY_,
+ outputX_,
+ 1,
+ 1,
+ confPaddingY_,
+ confPadding_);
+}
+
+} // namespace paddle
diff --git a/paddle/gserver/layers/MaxPoolWithMaskLayer.h b/paddle/gserver/layers/MaxPoolWithMaskLayer.h
new file mode 100644
index 0000000000000000000000000000000000000000..e0174add9d944930289f2bdf78d9f730fd1fcc7d
--- /dev/null
+++ b/paddle/gserver/layers/MaxPoolWithMaskLayer.h
@@ -0,0 +1,40 @@
+/* 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
+#include "PoolLayer.h"
+#include "paddle/math/Matrix.h"
+
+namespace paddle {
+/**
+ * @brief Basic parent layer of different kinds of pooling
+ */
+class MaxPoolWithMaskLayer : public PoolLayer {
+protected:
+ Argument mask_;
+
+public:
+ explicit MaxPoolWithMaskLayer(const LayerConfig& config)
+ : PoolLayer(config) {}
+
+ size_t getSize();
+
+ void forward(PassType passType) override;
+ void backward(const UpdateCallback& callback = nullptr) override;
+ bool init(const LayerMap& layerMap,
+ const ParameterMap& parameterMap) override;
+};
+} // namespace paddle
diff --git a/paddle/gserver/layers/PoolLayer.cpp b/paddle/gserver/layers/PoolLayer.cpp
index 7b932d5a76e9c4fe7cbe5882bbc19eb3de4b503a..87613a96c5b3c2da212f63e9e678bcd22308b08e 100644
--- a/paddle/gserver/layers/PoolLayer.cpp
+++ b/paddle/gserver/layers/PoolLayer.cpp
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "PoolLayer.h"
+#include "MaxPoolWithMaskLayer.h"
#include "PoolProjectionLayer.h"
#include "paddle/utils/Logging.h"
#ifdef PADDLE_WITH_CUDA
@@ -44,7 +45,6 @@ bool PoolLayer::init(const LayerMap& layerMap,
strideY_ = conf.has_stride_y() ? conf.stride_y() : conf.stride();
confPaddingY_ = conf.has_padding_y() ? conf.padding_y() : conf.padding();
outputY_ = conf.has_output_y() ? conf.output_y() : conf.output_x();
-
return true;
}
@@ -57,6 +57,8 @@ Layer* PoolLayer::create(const LayerConfig& config) {
} else if (CudnnPoolLayer::typeCheck(pool)) {
return new CudnnPoolLayer(config);
#endif
+ } else if (pool == "max-pool-with-mask") {
+ return new MaxPoolWithMaskLayer(config);
} else {
LOG(FATAL) << "Unknown pool type: " << pool;
return nullptr;
diff --git a/paddle/gserver/layers/ROIPoolLayer.cpp b/paddle/gserver/layers/ROIPoolLayer.cpp
index 35d4b12d3d357800fe72899069b5377c252fac5f..02402894d3354a6af221948a3360ef830881bf39 100644
--- a/paddle/gserver/layers/ROIPoolLayer.cpp
+++ b/paddle/gserver/layers/ROIPoolLayer.cpp
@@ -100,8 +100,9 @@ void ROIPoolLayer::forward(PassType passType) {
size_t roiEndH = round(bottomROIs[4] * spatialScale_);
CHECK_GE(roiBatchIdx, 0UL);
CHECK_LT(roiBatchIdx, batchSize);
- size_t roiHeight = std::max(roiEndH - roiStartH + 1, 1UL);
- size_t roiWidth = std::max(roiEndW - roiStartW + 1, 1UL);
+ size_t roiHeight =
+ std::max(roiEndH - roiStartH + 1, static_cast(1));
+ size_t roiWidth = std::max(roiEndW - roiStartW + 1, static_cast(1));
real binSizeH =
static_cast(roiHeight) / static_cast(pooledHeight_);
real binSizeW =
@@ -114,10 +115,14 @@ void ROIPoolLayer::forward(PassType passType) {
size_t wstart = static_cast(std::floor(pw * binSizeW));
size_t hend = static_cast(std::ceil((ph + 1) * binSizeH));
size_t wend = static_cast(std::ceil((pw + 1) * binSizeW));
- hstart = std::min(std::max(hstart + roiStartH, 0UL), height_);
- wstart = std::min(std::max(wstart + roiStartW, 0UL), width_);
- hend = std::min(std::max(hend + roiStartH, 0UL), height_);
- wend = std::min(std::max(wend + roiStartW, 0UL), width_);
+ hstart = std::min(
+ std::max(hstart + roiStartH, static_cast(0)), height_);
+ wstart = std::min(
+ std::max(wstart + roiStartW, static_cast(0)), width_);
+ hend = std::min(std::max(hend + roiStartH, static_cast(0)),
+ height_);
+ wend = std::min(std::max(wend + roiStartW, static_cast(0)),
+ width_);
bool isEmpty = (hend <= hstart) || (wend <= wstart);
size_t poolIndex = ph * pooledWidth_ + pw;
diff --git a/paddle/gserver/tests/CMakeLists.txt b/paddle/gserver/tests/CMakeLists.txt
index aa94ee406e27c86e6d49b6d2b5327a3f86bcacd6..4bea348f637f39444e8aad89278e6366ecd73b1d 100644
--- a/paddle/gserver/tests/CMakeLists.txt
+++ b/paddle/gserver/tests/CMakeLists.txt
@@ -1,9 +1,12 @@
# gserver pacakge unittests
add_simple_unittest(test_LinearChainCRF)
-add_simple_unittest(test_MultinomialSampler)
add_simple_unittest(test_RecurrentLayer)
+if(NOT MOBILE_INFERENCE)
+ add_simple_unittest(test_MultinomialSampler)
+endif()
+
function(gserver_test TARGET)
add_unittest_without_exec(${TARGET}
${TARGET}.cpp
@@ -24,6 +27,7 @@ gserver_test(test_ConvUnify)
gserver_test(test_BatchNorm)
gserver_test(test_KmaxSeqScore)
gserver_test(test_Expand)
+gserver_test(test_MaxPoolingWithMaskOutput)
########## test_Mkldnn layers and activations ##########
if(WITH_MKLDNN)
@@ -48,7 +52,7 @@ if(WITH_PYTHON)
endif()
############### test_WarpCTCLayer #######################
-if(NOT WITH_DOUBLE)
+if(NOT WITH_DOUBLE AND NOT MOBILE_INFERENCE)
add_unittest_without_exec(test_WarpCTCLayer
test_WarpCTCLayer.cpp)
diff --git a/paddle/gserver/tests/test_LayerGrad.cpp b/paddle/gserver/tests/test_LayerGrad.cpp
index fcbcb5b0f1f4cb07066363c9fa93fb1726459f30..3517d293e3c901caaa19952b04e56d1ef0d2b46e 100644
--- a/paddle/gserver/tests/test_LayerGrad.cpp
+++ b/paddle/gserver/tests/test_LayerGrad.cpp
@@ -434,7 +434,7 @@ void testConvLayer(const string& type, bool trans, bool useGpu) {
config.layerConfig.set_partial_sum(1);
config.layerConfig.set_shared_biases(true);
- int dilation = 1;
+ int dilation = 2;
if (type == "cudnn_conv") {
#if CUDNN_VERSION >= 6000
dilation = 2;
@@ -1234,6 +1234,7 @@ void testPoolLayer2(const string& poolType, bool trans, bool useGpu) {
TEST(Layer, PoolLayer) {
testPoolLayer("avg-projection", /* trans= */ false, /* useGpu= */ false);
testPoolLayer("max-projection", /* trans= */ false, /* useGpu= */ false);
+ testPoolLayer("max-pool-with-mask", /* trans= */ false, /* useGpu= */ false);
#ifdef PADDLE_WITH_CUDA
testPoolLayer("avg-projection", /* trans= */ false, /* useGpu= */ true);
@@ -1242,6 +1243,7 @@ TEST(Layer, PoolLayer) {
testPoolLayer("cudnn-avg-pool", /* trans= */ false, /* useGpu= */ true);
testPoolLayer2("cudnn-max-pool", /* trans= */ false, /* useGpu= */ true);
testPoolLayer2("cudnn-avg-pool", /* trans= */ false, /* useGpu= */ true);
+ testPoolLayer("max-pool-with-mask", /* trans= */ false, /* useGpu= */ true);
#endif
}
diff --git a/paddle/gserver/tests/test_MaxPoolingWithMaskOutput.cpp b/paddle/gserver/tests/test_MaxPoolingWithMaskOutput.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..16438886df94cab9d29d05924bb047e6c7f1f6fa
--- /dev/null
+++ b/paddle/gserver/tests/test_MaxPoolingWithMaskOutput.cpp
@@ -0,0 +1,117 @@
+/* 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
+
+#include "LayerGradUtil.h"
+#include "paddle/math/MathUtils.h"
+#include "paddle/testing/TestUtil.h"
+
+using namespace paddle;
+
+void setPoolConfig(TestConfig* config,
+ PoolConfig* pool,
+ const string& poolType) {
+ (*config).biasSize = 0;
+ (*config).layerConfig.set_type("pool");
+ (*config).layerConfig.set_num_filters(1);
+
+ int kw = 3, kh = 3;
+ int pw = 0, ph = 0;
+ int sw = 2, sh = 2;
+ pool->set_pool_type(poolType);
+ pool->set_channels(1);
+ pool->set_size_x(kw);
+ pool->set_size_y(kh);
+ pool->set_start(0);
+ pool->set_padding(pw);
+ pool->set_padding_y(ph);
+ pool->set_stride(sw);
+ pool->set_stride_y(sh);
+
+ int ow = outputSize(pool->img_size(), kw, pw, sw, /* caffeMode */ false);
+ int oh = outputSize(pool->img_size_y(), kh, ph, sh, /* caffeMode */ false);
+ pool->set_output_x(ow);
+ pool->set_output_y(oh);
+}
+
+void doOneMaxPoolingWithMaskOutputTest(MatrixPtr& inputMat,
+ const string& poolType,
+ bool use_gpu,
+ MatrixPtr& maskMat) {
+ TestConfig config;
+ config.inputDefs.push_back({INPUT_DATA, "layer_0", 25, 0});
+ LayerInputConfig* input = config.layerConfig.add_inputs();
+ PoolConfig* pool = input->mutable_pool_conf();
+
+ pool->set_img_size(5);
+ pool->set_img_size_y(5);
+ setPoolConfig(&config, pool, poolType);
+ config.layerConfig.set_size(pool->output_x() * pool->output_y() *
+ pool->channels());
+
+ config.layerConfig.set_name("MaxPoolWithMask");
+
+ std::vector dataLayers;
+ LayerMap layerMap;
+ vector datas;
+
+ initDataLayer(config,
+ &dataLayers,
+ &datas,
+ &layerMap,
+ "MaxPoolWithMask",
+ 1,
+ false,
+ use_gpu);
+
+ dataLayers[0]->getOutputValue()->copyFrom(*inputMat);
+
+ FLAGS_use_gpu = use_gpu;
+ std::vector parameters;
+ LayerPtr maxPoolingWithMaskOutputLayer;
+ initTestLayer(config, &layerMap, ¶meters, &maxPoolingWithMaskOutputLayer);
+ maxPoolingWithMaskOutputLayer->forward(PASS_GC);
+
+ checkMatrixEqual(maxPoolingWithMaskOutputLayer->getOutput("mask").value,
+ maskMat);
+}
+
+TEST(Layer, maxPoolingWithMaskOutputLayerFwd) {
+ bool useGpu = false;
+ MatrixPtr inputMat;
+ MatrixPtr maskMat;
+ real inputData[] = {0.1, 0.1, 0.5, 0.5, 1.1, 0.2, 0.2, 0.6, 0.1,
+ 0.1, 0.3, 0.3, 0.7, 0.1, 0.1, 0.4, 0.4, 0.8,
+ 0.8, 0.1, 1.0, 2.0, 3.0, 0.0, 9.0};
+ real maskData[] = {12, 4, 22, 24};
+
+ inputMat = Matrix::create(1, 25, false, useGpu);
+ maskMat = Matrix::create(1, 4, false, useGpu);
+ inputMat->setData(inputData);
+ maskMat->setData(maskData);
+ doOneMaxPoolingWithMaskOutputTest(
+ inputMat, "max-pool-with-mask", useGpu, maskMat);
+#ifdef PADDLE_WITH_CUDA
+ useGpu = true;
+ inputMat = Matrix::create(1, 25, false, useGpu);
+ maskMat = Matrix::create(1, 4, false, useGpu);
+ inputMat->copyFrom(inputData, 25);
+ maskMat->copyFrom(maskData, 4);
+ doOneMaxPoolingWithMaskOutputTest(
+ inputMat, "max-pool-with-mask", useGpu, maskMat);
+#endif
+}
diff --git a/paddle/math/BaseMatrix.cu b/paddle/math/BaseMatrix.cu
index 53dd5383601782231e6e742784007d1c9154dc6b..e3eff59dc575ee43552e401bc887f885a9804b61 100644
--- a/paddle/math/BaseMatrix.cu
+++ b/paddle/math/BaseMatrix.cu
@@ -1902,5 +1902,52 @@ void BaseMatrixT::sumOfProducts(BaseMatrixT& b,
}
template class BaseMatrixT;
+
+#ifndef PADDLE_MOBILE_INFERENCE
+
template class BaseMatrixT;
+
+#else
+
+template <>
+void BaseMatrixT::zero() {
+ applyUnary(unary::Zero());
+}
+
+template <>
+void BaseMatrixT::assign(int p) {
+ applyUnary(unary::Assign(p));
+}
+
+template <>
+void BaseMatrixT::isEqualTo(BaseMatrixT& b, int value) {
+ applyBinary(binary::IsEqual(value), b);
+}
+
+template <>
+void BaseMatrixT::neg() {
+ applyUnary(unary::Neg());
+}
+
+template <>
+void BaseMatrixT::abs2() {
+ applyUnary(unary::Abs());
+}
+
+template <>
+void BaseMatrixT::add(int p) {
+ applyUnary(unary::Add(p));
+}
+
+template <>
+void BaseMatrixT::add(int p1, int p2) {
+ applyUnary(unary::Add2(p1, p2));
+}
+
+template <>
+void BaseMatrixT::applyL1(int learningRate, int decayRate) {
+ applyUnary(unary::ApplyL1(learningRate * decayRate));
+}
+
+#endif
} // namespace paddle
diff --git a/paddle/math/CMakeLists.txt b/paddle/math/CMakeLists.txt
index 68b5296228cd733dc3cb7ca0f762e0a69187dbff..86bb270a4372841b3e6f4676e222d2190549c153 100644
--- a/paddle/math/CMakeLists.txt
+++ b/paddle/math/CMakeLists.txt
@@ -25,6 +25,19 @@ else()
message(STATUS "Compile with MKLDNNMatrix")
endif()
+if(MOBILE_INFERENCE)
+ list(REMOVE_ITEM MATH_SOURCES
+ ${CMAKE_CURRENT_SOURCE_DIR}/SIMDFunctions.cpp)
+ # Remove sparse
+ list(REMOVE_ITEM MATH_HEADERS
+ ${CMAKE_CURRENT_SOURCE_DIR}/CpuSparseMatrix.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/SparseMatrix.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/SparseRowMatrix.h)
+ list(REMOVE_ITEM MATH_SOURCES
+ ${CMAKE_CURRENT_SOURCE_DIR}/CpuSparseMatrix.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/SparseMatrix.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/SparseRowMatrix.cpp)
+endif()
set(MATH_SOURCES
"${PADDLE_SOURCE_DIR}/paddle/math/BaseMatrix.cu"
"${PADDLE_SOURCE_DIR}/paddle/math/TrainingAlgorithmOp.cu"
diff --git a/paddle/math/CpuSparseMatrix.h b/paddle/math/CpuSparseMatrix.h
index 36d57bbb65245de6b0de5909b55fbc4be3eccd78..aad1348353d558abca72ed0fa5cf943237e3ac78 100644
--- a/paddle/math/CpuSparseMatrix.h
+++ b/paddle/math/CpuSparseMatrix.h
@@ -13,6 +13,9 @@ See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
+
+#ifndef PADDLE_MOBILE_INFERENCE
+
#include
#include "Matrix.h"
@@ -309,3 +312,57 @@ private:
using Matrix::subMatrix;
};
} // namespace paddle
+
+#else
+
+#include "Matrix.h"
+
+namespace paddle {
+
+class CpuSparseMatrix : public Matrix {
+public:
+ CpuSparseMatrix(size_t height,
+ size_t width,
+ size_t nnz, /* used to allocate space */
+ SparseValueType valueType = FLOAT_VALUE,
+ SparseFormat format = SPARSE_CSR,
+ bool trans = false)
+ : Matrix(NULL, height, width, trans, false) {}
+
+ CpuSparseMatrix(real* data,
+ int* rows,
+ int* cols,
+ size_t height,
+ size_t width,
+ size_t nnz,
+ SparseValueType valueType,
+ SparseFormat format,
+ bool trans)
+ : Matrix(NULL, height, width, trans, false) {}
+
+ real* getValue() const { return nullptr; }
+ size_t getColStartIdx(size_t i) const { return 0; }
+ size_t getRowStartIdx(size_t i) const { return 0; }
+ size_t getColNum(size_t i) const { return 0; }
+ int* getRowCols(size_t i) const { return nullptr; }
+
+ CpuSparseMatrixPtr getTmpSparseMatrix(size_t height, size_t width) {
+ return nullptr;
+ }
+
+ void resize(size_t newHeight,
+ size_t newWidth,
+ size_t newNnz, /* used to allocate space */
+ SparseValueType valueType,
+ SparseFormat format) {}
+ void resize(size_t newHeight, size_t newWidth) {}
+ MatrixPtr getTranspose() { return nullptr; }
+ void setRow(size_t row,
+ size_t colNum,
+ const unsigned int* cols,
+ const real* values) {}
+};
+
+} // namespace paddle
+
+#endif
diff --git a/paddle/math/Matrix.cpp b/paddle/math/Matrix.cpp
index c3e34d5309d9ca8a32d7b0a8043e668cdb5be54b..88e9180690606c92cf46c5b295d80f14e5d64567 100644
--- a/paddle/math/Matrix.cpp
+++ b/paddle/math/Matrix.cpp
@@ -451,6 +451,7 @@ void GpuMatrix::addSharedBias(Matrix& b, real scale) {
}
void GpuMatrix::collectBias(Matrix& a, real scale) {
+#ifdef PADDLE_WITH_CUDA
CHECK_EQ(getHeight(), (size_t)1);
CHECK_EQ(width_, a.getWidth());
GpuSparseMatrix* sMatPtr = dynamic_cast(&a);
@@ -461,6 +462,7 @@ void GpuMatrix::collectBias(Matrix& a, real scale) {
hl_sparse_matrix_s A_d = sMatPtr->sMatrix_.get();
hl_sparse_matrix_column_sum(data, A_d, sMatPtr->getHeight(), width_, scale);
}
+#endif
}
void GpuMatrix::collectSharedBias(Matrix& a, real scale) {
@@ -552,6 +554,7 @@ void GpuMatrix::mul(const GpuSparseMatrix& a,
const GpuMatrix& b,
real scaleAB,
real scaleT) {
+#ifdef PADDLE_WITH_CUDA
CHECK(isContiguous());
CHECK(b.isContiguous());
CHECK(b.useGpu_ == true) << "Matrix type are not equal";
@@ -578,12 +581,14 @@ void GpuMatrix::mul(const GpuSparseMatrix& a,
b.height_,
scaleAB,
scaleT);
+#endif
}
void GpuMatrix::mul(const GpuMatrix& a,
const GpuSparseMatrix& b,
real scaleAB,
real scaleT) {
+#ifdef PADDLE_WITH_CUDA
CHECK(isContiguous());
CHECK(a.isContiguous());
CHECK(a.useGpu_ == true) << "Matrix type are not equal";
@@ -622,6 +627,7 @@ void GpuMatrix::mul(const GpuMatrix& a,
scaleAB,
scaleT);
}
+#endif
}
/* this = a*b */
@@ -1028,15 +1034,23 @@ void GpuMatrix::maxPoolForward(Matrix& inputMat,
size_t outputH,
size_t outputW,
size_t paddingH,
- size_t paddingW) {
+ size_t paddingW,
+ MatrixPtr maskMatP) {
CHECK(inputMat.useGpu_ == true) << "Matrix type are not equal";
real* inputData = inputMat.getData();
+ real* maskData = NULL;
size_t frameNum = inputMat.getHeight();
CHECK(imgSizeH * imgSizeW * channels == inputMat.getWidth());
CHECK(height_ == inputMat.getHeight());
CHECK(width_ == outputH * outputW * channels);
+ if (maskMatP != NULL) {
+ CHECK(maskMatP->useGpu_ == true) << "Matrix type are not equal";
+ CHECK(outputH * outputW * channels == maskMatP->getWidth());
+ maskData = maskMatP->getData();
+ }
+
hl_maxpool_forward(frameNum,
inputData,
channels,
@@ -1051,7 +1065,8 @@ void GpuMatrix::maxPoolForward(Matrix& inputMat,
paddingH,
paddingW,
data_,
- getStride());
+ getStride(),
+ maskData);
}
void GpuMatrix::maxPoolBackward(Matrix& inputMat,
@@ -1548,6 +1563,7 @@ void GpuMatrix::bilinearBackward(const Matrix& out,
}
void GpuMatrix::multiBinaryLabelCrossEntropy(Matrix& output, Matrix& label) {
+#ifdef PADDLE_WITH_CUDA
GpuMatrix* outputPtr = dynamic_cast(&output);
auto labelPtr = dynamic_cast(&label);
@@ -1563,9 +1579,11 @@ void GpuMatrix::multiBinaryLabelCrossEntropy(Matrix& output, Matrix& label) {
hl_sparse_matrix_s mat_d = labelPtr->sMatrix_.get();
hl_matrix_multi_binary_cross_entropy(
output_d, entropy_d, mat_d, height_, outputPtr->width_);
+#endif
}
void GpuMatrix::multiBinaryLabelCrossEntropyBp(Matrix& output, Matrix& label) {
+#ifdef PADDLE_WITH_CUDA
GpuMatrix* outputPtr = dynamic_cast(&output);
auto labelPtr = dynamic_cast(&label);
@@ -1581,6 +1599,7 @@ void GpuMatrix::multiBinaryLabelCrossEntropyBp(Matrix& output, Matrix& label) {
hl_sparse_matrix_s mat_d = labelPtr->sMatrix_.get();
hl_matrix_multi_binary_cross_entropy_bp(
output_d, grad_d, mat_d, height_, width_);
+#endif
}
void GpuMatrix::vol2Col(real* dataSrc,
@@ -1973,9 +1992,11 @@ void CpuMatrix::maxPoolForward(Matrix& inputMat,
size_t outputH,
size_t outputW,
size_t paddingH,
- size_t paddingW) {
+ size_t paddingW,
+ MatrixPtr maskMatP) {
real* inputData = inputMat.getData();
real* outData = data_;
+ real* maskData = NULL;
size_t num = inputMat.getHeight();
size_t inLength = imgSizeH * imgSizeW;
size_t outLength = outputH * outputW;
@@ -1984,6 +2005,11 @@ void CpuMatrix::maxPoolForward(Matrix& inputMat,
CHECK_EQ(channels * outLength, this->getWidth());
size_t outStride = getStride();
+ if (maskMatP != NULL) {
+ maskData = maskMatP->getData();
+ CHECK_EQ(channels * outLength, maskMatP->getWidth());
+ }
+
/* initialize the data_ */
for (size_t i = 0; i < height_; i++) {
for (size_t j = 0; j < width_; j++) {
@@ -2005,10 +2031,21 @@ void CpuMatrix::maxPoolForward(Matrix& inputMat,
int wstart = pw * strideW - paddingW;
int wend = std::min(wstart + sizeX, imgSizeW);
wstart = std::max(wstart, 0);
- for (int h = hstart; h < hend; ++h) {
- for (int w = wstart; w < wend; ++w) {
- outData[ph * outputW + pw] = std::max(
- outData[ph * outputW + pw], inputData[h * imgSizeW + w]);
+ if (maskData == NULL) {
+ for (int h = hstart; h < hend; ++h) {
+ for (int w = wstart; w < wend; ++w) {
+ outData[ph * outputW + pw] = std::max(
+ outData[ph * outputW + pw], inputData[h * imgSizeW + w]);
+ }
+ }
+ } else {
+ for (int h = hstart; h < hend; ++h) {
+ for (int w = wstart; w < wend; ++w) {
+ if (outData[ph * outputW + pw] < inputData[h * imgSizeW + w]) {
+ outData[ph * outputW + pw] = inputData[h * imgSizeW + w];
+ maskData[ph * outputW + pw] = h * imgSizeW + w;
+ }
+ }
}
}
}
@@ -2016,6 +2053,8 @@ void CpuMatrix::maxPoolForward(Matrix& inputMat,
// compute offset
inputData += inLength;
outData += outLength;
+
+ if (maskData != NULL) maskData += outLength;
}
}
}
@@ -3226,6 +3265,7 @@ template void CpuMatrix::mul(CpuSparseMatrix* a,
real scaleAB,
real scaleT);
+#ifndef PADDLE_MOBILE_INFERENCE
void SharedCpuMatrix::mul(CpuSparseMatrix* a,
CpuMatrix* b,
real scaleAB,
@@ -3354,6 +3394,7 @@ void SharedCpuMatrix::initBlock(int blockNum) {
}
}
+#endif
/* Add a (column) vector b to matrix a, column by column */
void CpuMatrix::addColumnVector(const Matrix& b) {
BaseMatrix::addColVector(const_cast(b));
diff --git a/paddle/math/Matrix.h b/paddle/math/Matrix.h
index 44180bca8bca53e74d71ce7bed3516399c01c81d..e273f1123690e31984c97185c5a8bc5e7b92c38c 100644
--- a/paddle/math/Matrix.h
+++ b/paddle/math/Matrix.h
@@ -861,7 +861,8 @@ public:
/**
* Pooling forward operation, pick out the largest element
- * in the sizeX of value
+ * in the sizeX of value, if the maskMatP is not NULL, it will
+ * also caculate the location indices.
*/
virtual void maxPoolForward(Matrix& inputMat,
size_t imgSizeH,
@@ -874,7 +875,8 @@ public:
size_t outputH,
size_t outputW,
size_t paddingH,
- size_t paddingW) {
+ size_t paddingW,
+ MatrixPtr maskMatP = NULL) {
LOG(FATAL) << "Not implemeted";
}
@@ -1426,7 +1428,8 @@ public:
size_t outputH,
size_t outputW,
size_t paddingH,
- size_t paddingW);
+ size_t paddingW,
+ MatrixPtr maskMatP);
void maxPoolBackward(Matrix& image,
size_t imgSizeH,
@@ -1697,7 +1700,8 @@ public:
size_t outputH,
size_t outputW,
size_t paddingH,
- size_t paddingW);
+ size_t paddingW,
+ MatrixPtr maskMatP);
void maxPoolBackward(Matrix& image,
size_t imgSizeH,
@@ -2066,6 +2070,7 @@ public:
class SharedCpuMatrix : public CpuMatrix {
public:
+#ifndef PADDLE_MOBILE_INFERENCE
/* blockNum is number of partitions of the matrix */
SharedCpuMatrix(int blockNum, size_t height, size_t width, bool trans = false)
: CpuMatrix(height, width, trans) {
@@ -2111,6 +2116,7 @@ private:
ThreadLocal localBuf_;
ThreadLocal> localBufRows_;
ThreadLocal> blockSeq_;
+#endif
};
typedef struct { unsigned int col; } sparse_non_value_t;
diff --git a/paddle/math/SparseMatrix.h b/paddle/math/SparseMatrix.h
index 16300db081f89182faa82ea5798e8ec2f1cd93f9..e0a3c6d2286521f6030867b747099514a16df5cf 100644
--- a/paddle/math/SparseMatrix.h
+++ b/paddle/math/SparseMatrix.h
@@ -13,6 +13,9 @@ See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
+
+#ifndef PADDLE_MOBILE_INFERENCE
+
#include
#include "CpuSparseMatrix.h"
#include "Matrix.h"
@@ -237,3 +240,47 @@ private:
};
} // namespace paddle
+
+#else
+
+#include "CpuSparseMatrix.h"
+
+namespace paddle {
+
+class GpuSparseMatrix : public Matrix {
+public:
+ GpuSparseMatrix(size_t height,
+ size_t width,
+ size_t nnz, /* used to allocate space */
+ SparseValueType valueType = FLOAT_VALUE,
+ SparseFormat format_ = SPARSE_CSR,
+ bool trans = false)
+ : Matrix(NULL, height, width, trans, false) {}
+
+ GpuSparseMatrix(real* value,
+ int* rows,
+ int* cols,
+ size_t height,
+ size_t width,
+ size_t nnz,
+ SparseValueType valueType,
+ SparseFormat format,
+ bool trans)
+ : Matrix(NULL, height, width, trans, true) {}
+
+ void resize(size_t newHeight,
+ size_t newWidth,
+ size_t newNnz, /* used to allocate space */
+ SparseValueType valueType,
+ SparseFormat format) {}
+ void resize(size_t newHeight, size_t newWidth) {}
+ MatrixPtr getTranspose() { return nullptr; }
+ void setRow(size_t row,
+ size_t colNum,
+ const unsigned int* cols,
+ const real* values) {}
+};
+
+} // namespace paddle
+
+#endif
diff --git a/paddle/math/SparseRowMatrix.h b/paddle/math/SparseRowMatrix.h
index 8704eb038d5d42ca834d232c0a651e9ffb2b40f3..ca7a6806da3a58ad5fffdbb6505319964c25bc6f 100644
--- a/paddle/math/SparseRowMatrix.h
+++ b/paddle/math/SparseRowMatrix.h
@@ -14,6 +14,8 @@ limitations under the License. */
#pragma once
+#ifndef PADDLE_MOBILE_INFERENCE
+
#include
#include
#include
@@ -313,3 +315,27 @@ private:
};
} // namespace paddle
+
+#else
+namespace paddle {
+
+class SparseRowCpuMatrix : public CpuMatrix {
+public:
+ void reserveStore() {}
+ void clearIndices() {}
+};
+
+class SparsePrefetchRowCpuMatrix : public SparseRowCpuMatrix {
+public:
+ void setupIndices() {}
+ void addRows(MatrixPtr input) {}
+ void addRows(IVectorPtr ids) {}
+};
+
+class SparseAutoGrowRowCpuMatrix : public SparseRowCpuMatrix {};
+class CacheRowCpuMatrix : public SparseAutoGrowRowCpuMatrix {};
+class SparseRowIdsCpuMatrix : public CpuMatrix {};
+
+} // namespace paddle
+
+#endif
diff --git a/paddle/math/tests/CMakeLists.txt b/paddle/math/tests/CMakeLists.txt
index ceb96b2e250d8e04ffb2b1d8c77ad498dca91cf3..d8b7f9e3fc74040189ade83049e4a1c3348e08de 100644
--- a/paddle/math/tests/CMakeLists.txt
+++ b/paddle/math/tests/CMakeLists.txt
@@ -3,8 +3,10 @@
add_simple_unittest(test_ExecViaCpu)
add_simple_unittest(test_SIMDFunctions)
add_simple_unittest(test_TrainingAlgorithm)
-add_simple_unittest(test_SparseMatrix)
add_simple_unittest(test_RowBuffer)
+if(NOT MOBILE_INFERENCE)
+ add_simple_unittest(test_SparseMatrix)
+endif()
# TODO(yuyang18): Refactor TestUtil.cpp. Remove this cross module reference.
add_unittest(test_matrixCompare
diff --git a/paddle/memory/README.md b/paddle/memory/README.md
index 7f95e80f980b0c0b93ecb418e6b923045313eaa5..6cb003c50bc7d142d65b0591e7e5235431d2ea42 100644
--- a/paddle/memory/README.md
+++ b/paddle/memory/README.md
@@ -1,4 +1,141 @@
# Region-based Heterogeneous Memory Management
+## Design
-Please check out the [design documentation](http://gangliao.me) to find out more details about
-buddy memory allocator for both CPU and GPU.
+### Usage
+
+To allocate 4KB CPU memory:
+
+```cpp
+p = memory::Alloc(platform::CPUPlace(), 4*1024);
+```
+
+To allocate 4KB memory on the 3rd GPU:
+
+```cpp
+p = memory::Alloc(platform::GPUPlace(2), 4*1024);
+```
+
+To free memory and check the so-far used amount of memory on a place:
+
+```cpp
+auto pl = platform::GPUPlace(0);
+p = memory::Alloc(pl, 4*1024);
+cout << memory::Used(pl);
+memory::Free(pl, p);
+```
+
+### API
+
+In `paddle/memory/memory.h` we have:
+
+```cpp
+namespace memory {
+template void* Alloc(Place, size_t);
+template void Free(Place, void*);
+template size_t Used(Place);
+} // namespace memory
+```
+
+These function templates have specializations on either `platform::CPUPlace` or `platform::GPUPlace`:
+
+```cpp
+template<>
+void* Alloc(CPUPlace p, size_t size) {
+ return GetCPUBuddyAllocator()->Alloc(size);
+}
+```
+
+and
+
+```cpp
+template<>
+void Alloc(GPUPlace p, size_t size) {
+ return GetGPUBuddyAllocator(p.id)->Alloc(size);
+}
+```
+
+Similar specializations exist for `Free` and `Used`.
+
+### Implementation
+
+`GetCPUBuddyAllocator` and `GetGPUBuddyAllocator` are singletions.
+
+```cpp
+BuddyAllocator* GetCPUBuddyAllocator() {
+ static BuddyAllocator* a = NULL;
+ if (a == NULL) {
+ a = new BuddyAllocator(new CPUAllocator /*backup allocator*/, ...);
+ }
+ return a;
+}
+
+BuddyAllocator* GetGPUBuddyAllocator(int gpu_id) {
+ static BuddyAllocator* as = NULL;
+ if (as == NULL) {
+ as = new BuddyAllocator*[platform::NumGPUs()];
+ for (int gpu = 0; gpu < platform::NumGPUs(); gpu++) {
+ as[gpu] = new BuddyAllocator(new GPUAllocator(gpu) /* backup allocator */, ...);
+ }
+ }
+ return as[gpu_id);
+```
+
+#### `BuddyAllocator`
+
+`BuddyAllocator` implements the buddy allocation algorithm. Its constructor takes parameters only related with the algorithm:
+
+```cpp
+BuddyAllocator::BuddyAllocator(initial_pool_size, max_pool_size) {
+ ...
+}
+```
+
+Please be aware that **`BuddyAllocator` always allocate aligned memory**, aligned on 32-bytes, which can hold a `BuddyAllocator::Block` object:
+
+```cpp
+class BuddyAllocator {
+ private:
+ struct Block {
+ size_t size;
+ Block* left, right;
+ size_t index; // allocator id
+ };
+ ...
+};
+```
+
+Because BuddyAllocator has the meta-data of each block, it can trace the used memory -- record the amount returned by `Alloc` freed in `Free`. Instead, `CPUAllocator` and `GPUAllocator` doesn't know the size of freed memory block and cannot do the trace.
+
+#### System Allocators
+
+The `GPUAllocator` and `CPUAllocator` are calls *system allocators*. They work as the fallback allocators of `BuddyAllocator`.
+
+## Justification
+
+I got inspiration from Majel and Caffe2, though above design look different from both.
+
+### Caffe2
+
+In Caffe2, `Tensor::mutable_data()` allocates the memroy. In particular, [`Tensor::mutable_data`](https://github.com/caffe2/caffe2/blob/v0.7.0/caffe2/core/tensor.h#L523) calls [`Tensor::raw_mutable_data`](https://github.com/caffe2/caffe2/blob/v0.7.0/caffe2/core/tensor.h#L459), which in turn calls [`Context::New`](https://github.com/caffe2/caffe2/blob/v0.7.0/caffe2/core/tensor.h#L479).
+
+There are two implementations of `Context`:
+
+1. [`CPUContext`](https://github.com/caffe2/caffe2/blob/v0.7.0/caffe2/core/context.h#L105), whose [`New` method](https://github.com/caffe2/caffe2/blob/v0.7.0/caffe2/core/context.h#L131) calls [`g_cpu_allocator.get()->New(size_t)`](https://github.com/caffe2/caffe2/blob/v0.7.0/caffe2/core/context.cc#L15) to allocate the memory.
+
+1. [`CUDAContext`](https://github.com/caffe2/caffe2/blob/v0.7.0/caffe2/core/context_gpu.h#L99), which has a data member [`int gpu_id_`](https://github.com/caffe2/caffe2/blob/v0.7.0/caffe2/core/context_gpu.h#L202). This looks very similar to class `majel::GPUPlace`, who also has an `int id_` data member. `CUDAContext::New(size_t)` calls [`g_cub_allocator->DeviceAllocate(&ptr, nbytes)`](https://github.com/caffe2/caffe2/blob/v0.7.0/caffe2/core/context_gpu.cu#L355) to allocate the memory.
+
+### Majel
+
+In Majel, there are basically two allocator types:
+
+1. `cpu::SystemAllocator`, which has similar functionality to `caffe2::CPUContext::New/Delete`.
+1. `gpu::SystemAllocator`, which has similar functionality to `caffe2::CUDAContext::New/Delete`.
+
+However, memory allocation is not via these two allocators. Instead, these two allocators are defined in hidden namespaces.
+
+In Majel there are hidden global variables like:
+
+1. `cpu::SystemAllocator g_cpu_allocator`, and
+1. `vector g_gpu_allocators(NUM_GPUS)`.
+
+Programs allocate memory via a BuddyAllocator, which can take the `g_cpu_allocator` or a `g_gpu_allocators[gpu_id]` as its *fallback allocator*, so that if BuddyAllocator cannot find a block in its memory pool, it extends its memory pool by calling the fallback allocator's `New(size_t)`.
diff --git a/paddle/operators/CMakeLists.txt b/paddle/operators/CMakeLists.txt
index 709f7de2e43093114d096cbfca5b5d49293a6d3e..a719da2560291dbc7e98aadfae41d4692d8afcad 100644
--- a/paddle/operators/CMakeLists.txt
+++ b/paddle/operators/CMakeLists.txt
@@ -9,6 +9,7 @@ function(op_library TARGET)
set(OP_LIBRARY ${TARGET} ${OP_LIBRARY} PARENT_SCOPE)
set(cc_srcs)
set(cu_srcs)
+ set(cu_cc_srcs)
set(op_common_deps operator op_registry math_function)
set(options "")
set(oneValueArgs "")
@@ -22,6 +23,9 @@ function(op_library TARGET)
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cc)
list(APPEND cc_srcs ${TARGET}.cc)
endif()
+ if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cu.cc)
+ list(APPEND cu_cc_srcs ${TARGET}.cu.cc)
+ endif()
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cu)
list(APPEND cu_srcs ${TARGET}.cu)
endif()
@@ -29,6 +33,8 @@ function(op_library TARGET)
foreach(src ${op_library_SRCS})
if (${src} MATCHES ".*\\.cu$")
list(APPEND cu_srcs ${src})
+ elseif(${src} MATCHES ".*\\.cu.cc$")
+ list(APPEND cu_cc_srcs ${src})
elseif(${src} MATCHES ".*\\.cc$")
list(APPEND cc_srcs ${src})
else()
@@ -43,7 +49,7 @@ function(op_library TARGET)
endif()
if (WITH_GPU)
- nv_library(${TARGET} SRCS ${cc_srcs} ${cu_srcs} DEPS ${op_library_DEPS}
+ nv_library(${TARGET} SRCS ${cc_srcs} ${cu_cc_srcs} ${cu_srcs} DEPS ${op_library_DEPS}
${op_common_deps})
else()
cc_library(${TARGET} SRCS ${cc_srcs} DEPS ${op_library_DEPS}
@@ -140,7 +146,9 @@ function(op_library TARGET)
# pybind USE_CPU_ONLY_OP
list(LENGTH cu_srcs cu_srcs_len)
- if (${pybind_flag} EQUAL 0 AND ${cu_srcs_len} EQUAL 0)
+ list(LENGTH cu_cc_srcs cu_cc_srcs_len)
+
+ if (${pybind_flag} EQUAL 0 AND ${cu_srcs_len} EQUAL 0 AND ${cu_cc_srcs_len} EQUAL 0)
file(APPEND ${pybind_file} "USE_CPU_ONLY_OP(${TARGET});\n")
set(pybind_flag 1)
endif()
@@ -160,11 +168,12 @@ set(DEPS_OPS
recurrent_op
dynamic_recurrent_op
softmax_with_cross_entropy_op
+ softmax_op
+ sequence_softmax_op
sum_op
pool_op
pool_with_index_op
conv_op
- lstm_op
conv_transpose_op
nccl_op
sequence_conv_op
@@ -174,13 +183,20 @@ set(DEPS_OPS
array_to_lod_tensor_op
lstm_op
tensor_array_read_write_op
- gru_op)
+ gru_op
+ adagrad_op
+ sgd_op)
+
op_library(cond_op SRCS cond_op.cc DEPS framework_proto tensor operator net_op)
op_library(cross_entropy_op DEPS cross_entropy)
op_library(softmax_with_cross_entropy_op DEPS cross_entropy softmax)
+op_library(softmax_op DEPS softmax)
+op_library(sequence_softmax_op DEPS softmax)
+op_library(sum_op DEPS selected_rows_functor)
+op_library(sgd_op DEPS selected_rows_functor)
+op_library(adagrad_op DEPS selected_rows_functor)
op_library(conv_op DEPS vol2col)
-op_library(sum_op DEPS net_op selected_rows_functor)
op_library(pool_op DEPS pooling)
op_library(pool_with_index_op DEPS pooling)
op_library(lod_rank_table_op SRCS lod_rank_table_op.cc DEPS lod_rank_table)
@@ -220,6 +236,6 @@ cc_test(dynamic_recurrent_op_test SRCS dynamic_recurrent_op_test.cc
rnn/recurrent_op_utils.cc
DEPS dynamic_recurrent_op)
if(WITH_GPU)
- nv_test(nccl_op_test SRCS nccl_op_test.cu DEPS nccl_op gpu_info device_context)
+ cc_test(nccl_op_test SRCS nccl_op_test.cu.cc DEPS nccl_op gpu_info device_context)
endif()
cc_test(save_load_op_test SRCS save_load_op_test.cc DEPS save_op load_op)
diff --git a/paddle/operators/accuracy_op.cc b/paddle/operators/accuracy_op.cc
index 03c2fa945d94a522d25e65103c8842a93852ba3d..2785a8c6fb62527db4d203788be88ebead068a19 100644
--- a/paddle/operators/accuracy_op.cc
+++ b/paddle/operators/accuracy_op.cc
@@ -30,6 +30,10 @@ class AccuracyOp : public framework::OperatorWithKernel {
"Input (Label) of accuracy op should not be null.");
PADDLE_ENFORCE(ctx->HasOutput("Accuracy"),
"Output (Accuracy) of AccuracyOp should not be null.");
+ PADDLE_ENFORCE(ctx->HasOutput("Correct"),
+ "Output (Correct) of AccuracyOp should not be null.");
+ PADDLE_ENFORCE(ctx->HasOutput("Total"),
+ "Output (Total) of AccuracyOp should not be null.");
auto inference_dim = ctx->GetInputDim("Out");
auto label_dim = ctx->GetInputDim("Label");
@@ -43,6 +47,8 @@ class AccuracyOp : public framework::OperatorWithKernel {
" the same as label.");
ctx->SetOutputDim("Accuracy", {1});
+ ctx->SetOutputDim("Correct", {1});
+ ctx->SetOutputDim("Total", {1});
ctx->ShareLoD("Out", /*->*/ "Accuracy");
}
@@ -66,6 +72,8 @@ class AccuracyOpMaker : public framework::OpProtoAndCheckerMaker {
AddInput("Label", "Label of the training data");
// TODO(typhoonzero): AddInput("Weight", ...
AddOutput("Accuracy", "The accuracy of current batch");
+ AddOutput("Correct", "The correct samples count of current batch");
+ AddOutput("Total", "The samples count of current batch");
AddComment(R"DOC(
Accuracy Operator.
diff --git a/paddle/operators/accuracy_op.cu b/paddle/operators/accuracy_op.cu
index 1776f33105367447759aa91c25263dfc53bd2f99..d2dcab4e548b99c6beecfaa570ac31804fd07d82 100644
--- a/paddle/operators/accuracy_op.cu
+++ b/paddle/operators/accuracy_op.cu
@@ -16,6 +16,7 @@ limitations under the License. */
#include
#include "paddle/operators/accuracy_op.h"
#include "paddle/platform/cuda_helper.h"
+#include "paddle/platform/gpu_info.h"
namespace paddle {
namespace operators {
@@ -24,7 +25,8 @@ using platform::PADDLE_CUDA_NUM_THREADS;
template
__global__ void AccuracyCudaKernel(const int N, const int D,
const int64_t* Xdata,
- const int64_t* labeldata, float* accuracy) {
+ const int64_t* labeldata, int* correct_data,
+ float* accuracy) {
int count = 0;
__shared__ int total[BlockSize];
@@ -43,6 +45,7 @@ __global__ void AccuracyCudaKernel(const int N, const int D,
// reduce the count with init value 0, and output accuracy.
int result = thrust::reduce(thrust::device, total, total + BlockSize, 0);
if (threadIdx.x == 0) {
+ *correct_data = result;
*accuracy = static_cast(result) / static_cast(N);
}
}
@@ -56,31 +59,50 @@ class AccuracyOpCUDAKernel : public framework::OpKernel {
auto* inference = ctx.Input("Out");
auto* indices = ctx.Input("Indices");
auto* label = ctx.Input("Label");
+
auto* accuracy = ctx.Output("Accuracy");
+ auto* correct = ctx.Output("Correct");
+ auto* total = ctx.Output("Total");
// FIXME(typhoonzero): only support indices currently
// if add support for output values, how to detect the data type?
const int64_t* indices_data = indices->data();
const int64_t* label_data = label->data();
+
+ int* correct_data = correct->mutable_data(ctx.GetPlace());
+ int* total_data = total->mutable_data(ctx.GetPlace());
float* accuracy_data = accuracy->mutable_data(ctx.GetPlace());
- size_t num_samples = inference->dims()[0];
+ int num_samples = static_cast(inference->dims()[0]);
size_t infer_width = inference->dims()[1];
- PADDLE_ENFORCE(cudaMemset(accuracy_data, 0, sizeof(float)));
+ auto stream = ctx.cuda_device_context().stream();
+ platform::GpuMemsetAsync(accuracy_data, 0, sizeof(float), stream);
if (num_samples == 0) {
return;
}
+ platform::GpuMemcpyAsync(total_data, &num_samples, sizeof(int),
+ cudaMemcpyHostToDevice, stream);
+
+ AccuracyCudaKernel<
+ PADDLE_CUDA_NUM_THREADS><<<1, PADDLE_CUDA_NUM_THREADS, 0, stream>>>(
+ num_samples, infer_width, indices_data, label_data, correct_data,
+ accuracy_data);
- AccuracyCudaKernel<<<
- 1, PADDLE_CUDA_NUM_THREADS, 0, ctx.cuda_device_context().stream()>>>(
- num_samples, infer_width, indices_data, label_data, accuracy_data);
+ int d_num_samples, d_num_correct;
+ float d_accuracy;
+ platform::GpuMemcpyAsync(&d_num_correct, correct_data, sizeof(int),
+ cudaMemcpyDeviceToHost, stream);
+ platform::GpuMemcpyAsync(&d_num_samples, total_data, sizeof(int),
+ cudaMemcpyDeviceToHost, stream);
+ platform::GpuMemcpyAsync(&d_accuracy, accuracy_data, sizeof(float),
+ cudaMemcpyDeviceToHost, stream);
}
};
} // namespace operators
} // namespace paddle
-// FIXME(typhoonzero): types of T is for infernece data.
-// label data is always int
+// FIXME(typhoonzero): types of T is for inference data.
+// label data is always int64
REGISTER_OP_GPU_KERNEL(accuracy, paddle::operators::AccuracyOpCUDAKernel,
paddle::operators::AccuracyOpCUDAKernel);
diff --git a/paddle/operators/accuracy_op.h b/paddle/operators/accuracy_op.h
index 28dbc77f64842a62e88ae8df4ead7adc3b03764b..d060e6edddb31ecc1a4d27836f80b8ac5fa7d36d 100644
--- a/paddle/operators/accuracy_op.h
+++ b/paddle/operators/accuracy_op.h
@@ -29,7 +29,11 @@ class AccuracyKernel : public framework::OpKernel {
auto* indices = ctx.Input("Indices");
auto* label = ctx.Input("Label");
auto* accuracy = ctx.Output("Accuracy");
+ auto* correct = ctx.Output("Correct");
+ auto* total = ctx.Output("Total");
+ int* correct_data = correct->mutable_data(ctx.GetPlace());
+ int* total_data = total->mutable_data(ctx.GetPlace());
float* accuracy_data = accuracy->mutable_data(ctx.GetPlace());
const int64_t* indices_data = indices->data();
@@ -55,7 +59,8 @@ class AccuracyKernel : public framework::OpKernel {
}
}
- // FIXME(typhoonzero): we don't accumulate the accuracy for now.
+ *correct_data = num_correct;
+ *total_data = num_samples;
*accuracy_data =
static_cast(num_correct) / static_cast(num_samples);
}
diff --git a/paddle/operators/adagrad_op.cc b/paddle/operators/adagrad_op.cc
index 8d1a2b7938d2c6607cbeb3cecb72d1d5b83dd8b9..d6686e3ef3165976cf4c077a7a0f213082aa7716 100644
--- a/paddle/operators/adagrad_op.cc
+++ b/paddle/operators/adagrad_op.cc
@@ -14,6 +14,11 @@ limitations under the License. */
#include "paddle/operators/adagrad_op.h"
+#include
+
+#include "paddle/operators/math/math_function.h"
+#include "paddle/operators/math/selected_rows_functor.h"
+
namespace paddle {
namespace operators {
@@ -21,7 +26,7 @@ class AdagradOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
- void InferShape(framework::InferShapeContext *ctx) const override {
+ void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("Param"),
"Input(Param) of AdagradOp should not be null.");
PADDLE_ENFORCE(ctx->HasInput("Grad"),
@@ -54,8 +59,8 @@ class AdagradOp : public framework::OperatorWithKernel {
class AdagradOpMaker : public framework::OpProtoAndCheckerMaker {
public:
- AdagradOpMaker(framework::OpProto *proto,
- framework::OpAttrChecker *op_checker)
+ AdagradOpMaker(framework::OpProto* proto,
+ framework::OpAttrChecker* op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("Param", "(Tensor) Input parameter");
AddInput("Grad", "(Tensor) Input gradient");
@@ -87,10 +92,85 @@ for numerical stability to avoid the division by zero error.
)DOC");
}
};
+
+namespace {
+size_t FindPos(const std::vector& rows, int64_t value) {
+ return std::find(rows.begin(), rows.end(), value) - rows.begin();
+}
+} // namespace
+
+template
+struct SparseAdagradFunctor {
+ void operator()(const platform::DeviceContext& context,
+ const framework::SelectedRows& grad,
+ const framework::Tensor& learning_rate, T epsilon,
+ framework::Tensor* moment, framework::Tensor* param) {
+ // 1. g_m.rows = set(g.rows)
+ auto grad_rows = grad.rows();
+ std::set row_set(grad_rows.begin(), grad_rows.end());
+ std::vector merge_rows(row_set.begin(), row_set.end());
+
+ auto grad_width = grad.value().dims()[1];
+ std::unique_ptr grad_merge{
+ new framework::SelectedRows()};
+ grad_merge->set_rows(merge_rows);
+ grad_merge->set_height(grad.height());
+ grad_merge->mutable_value()->mutable_data(
+ framework::make_ddim(
+ {static_cast(merge_rows.size()), grad_width}),
+ context.GetPlace());
+
+ math::SetConstant constant_functor;
+ constant_functor(context, grad_merge->mutable_value(), 0.0);
+
+ auto* grad_merge_data = grad_merge->mutable_value()->data();
+ auto* grad_data = grad.value().data();
+
+ for (size_t i = 0; i < grad_rows.size(); i++) {
+ size_t grad_merge_i = FindPos(merge_rows, grad_rows[i]);
+ for (int64_t j = 0; j < grad_width; j++) {
+ grad_merge_data[grad_merge_i * grad_width + j] +=
+ grad_data[i * grad_width + j];
+ }
+ }
+
+ // 2. m += g_m * g_m
+ std::unique_ptr grad_square{
+ new framework::SelectedRows()};
+ grad_square->set_rows(grad_merge->rows());
+ grad_square->set_height(grad_merge->height());
+ grad_square->mutable_value()->mutable_data(grad_merge->value().dims(),
+ context.GetPlace());
+ auto gs =
+ framework::EigenVector::Flatten(*(grad_square->mutable_value()));
+ auto gm = framework::EigenVector::Flatten(grad_merge->value());
+ gs.device(*context.GetEigenDevice()) = gm * gm;
+
+ math::SelectedRowsAddToTensor functor;
+ functor(context, *grad_square, moment);
+
+ // 3. update parameter
+ auto* lr = learning_rate.data();
+ auto* param_data = param->data();
+ auto* moment_data = moment->data();
+
+ for (size_t i = 0; i < merge_rows.size(); i++) {
+ for (int64_t j = 0; j < grad_width; j++) {
+ param_data[merge_rows[i] * grad_width + j] -=
+ lr[0] * grad_merge_data[i * grad_width + j] /
+ (std::sqrt(moment_data[merge_rows[i] * grad_width + j]) + epsilon);
+ }
+ }
+ }
+};
+
+template struct SparseAdagradFunctor;
+template struct SparseAdagradFunctor;
} // namespace operators
} // namespace paddle
namespace ops = paddle::operators;
REGISTER_OP_WITHOUT_GRADIENT(adagrad, ops::AdagradOp, ops::AdagradOpMaker);
-REGISTER_OP_CPU_KERNEL(adagrad,
- ops::AdagradOpKernel);
+REGISTER_OP_CPU_KERNEL(
+ adagrad, ops::AdagradOpKernel,
+ ops::AdagradOpKernel);
diff --git a/paddle/operators/adagrad_op.cu b/paddle/operators/adagrad_op.cu
index a5b7951121360f78612f9008a522235104708112..5b869e6bc5f4604ba6055ffd62fa21e4a1f41b93 100644
--- a/paddle/operators/adagrad_op.cu
+++ b/paddle/operators/adagrad_op.cu
@@ -14,7 +14,138 @@
#define EIGEN_USE_GPU
#include "paddle/operators/adagrad_op.h"
+#include "paddle/operators/math/selected_rows_functor.h"
+#include "paddle/operators/math/math_function.h"
+#include "paddle/platform/cuda_helper.h"
+
+namespace paddle {
+namespace operators {
+
+namespace {
+
+template
+__global__ void MergeGradKernel(const T* grad, const int64_t* grad_rows,
+ T* grad_merge, const int64_t* grad_merge_rows,
+ size_t grad_merge_rows_size,
+ int64_t row_numel) {
+ const int ty = blockIdx.y;
+ int tid = threadIdx.x;
+ __shared__ size_t grad_merge_idx;
+
+ if (tid == 0) {
+ for (size_t i = 0; i < grad_merge_rows_size; i++) {
+ if (grad_rows[ty] == grad_merge_rows[i]) {
+ grad_merge_idx = i;
+ }
+ }
+ }
+
+ __syncthreads();
+
+ grad += ty * row_numel;
+ grad_merge += grad_merge_idx * row_numel;
+ for (int index = tid; index < row_numel; index += block_size) {
+ paddle::platform::CudaAtomicAdd(grad_merge + index, grad[index]);
+ }
+}
+
+template
+__global__ void SparseAdagradFunctorKernel(const T* grad, const int64_t* rows,
+ const T* learning_rate, T* param,
+ T* moment, int64_t row_numel,
+ T epsilon) {
+ const int ty = blockIdx.y;
+ int tid = threadIdx.x;
+
+ grad += ty * row_numel;
+ param += rows[ty] * row_numel;
+ moment += rows[ty] * row_numel;
+
+ for (int index = tid; index < row_numel; index += block_size) {
+ // Since index in rows of SelectedRows can be duplicate, we have to use
+ // Atomic Operation to avoid concurrent write error.
+ paddle::platform::CudaAtomicAdd(param + index,
+ -1.0 * learning_rate[0] * grad[index] /
+ (sqrt(moment[index]) + epsilon));
+ }
+}
+} // namespace
+
+template
+struct SparseAdagradFunctor {
+ void operator()(const platform::DeviceContext& context,
+ const framework::SelectedRows& grad,
+ const framework::Tensor& learning_rate, T epsilon,
+ framework::Tensor* moment, framework::Tensor* param) {
+ // 1. g_m.rows = set(g.rows)
+ auto grad_rows = grad.rows();
+ std::set row_set(grad_rows.begin(), grad_rows.end());
+ std::vector merge_rows(row_set.begin(), row_set.end());
+
+ auto grad_width = grad.value().dims()[1];
+ std::unique_ptr grad_merge{
+ new framework::SelectedRows()};
+ grad_merge->set_rows(merge_rows);
+ grad_merge->set_height(grad.height());
+ grad_merge->mutable_value()->mutable_data(
+ framework::make_ddim(
+ {static_cast(merge_rows.size()), grad_width}),
+ context.GetPlace());
+
+ math::SetConstant constant_functor;
+ constant_functor(context, grad_merge->mutable_value(), 0.0);
+
+ auto* grad_merge_data = grad_merge->mutable_value()->data();
+ auto* grad_data = grad.value().data();
+
+ const int block_size = 256;
+ dim3 threads(block_size, 1);
+ dim3 grid1(1, grad_rows.size());
+
+ MergeGradKernel<
+ T, 256><<(context)
+ .stream()>>>(grad_data, grad.rows().data(),
+ grad_merge_data, grad_merge->rows().data(),
+ grad_merge->rows().size(), grad_width);
+
+ // 2. m += g_m * g_m
+ std::unique_ptr grad_square{
+ new framework::SelectedRows()};
+ grad_square->set_rows(grad_merge->rows());
+ grad_square->set_height(grad_merge->height());
+ grad_square->mutable_value()->mutable_data(grad_merge->value().dims(),
+ context.GetPlace());
+ auto gs =
+ framework::EigenVector::Flatten(*(grad_square->mutable_value()));
+ auto gm = framework::EigenVector::Flatten(grad_merge->value());
+ gs.device(*context.GetEigenDevice()) = gm * gm;
+
+ math::SelectedRowsAddToTensor functor;
+ functor(context, *grad_square, moment);
+
+ // 3. update parameter
+ auto* lr = learning_rate.data();
+ auto* param_data = param->data();
+ auto* moment_data = moment->data();
+
+ dim3 grid2(1, merge_rows.size());
+ SparseAdagradFunctorKernel<
+ T, 256><<(context)
+ .stream()>>>(grad_merge_data, grad_merge->rows().data(),
+ lr, param_data,
+ moment_data, grad_width, epsilon);
+ }
+};
+
+template struct SparseAdagradFunctor;
+template struct SparseAdagradFunctor;
+
+} // namespace operators
+} // namespace paddle
namespace ops = paddle::operators;
-REGISTER_OP_GPU_KERNEL(adagrad,
- ops::AdagradOpKernel);
+REGISTER_OP_GPU_KERNEL(
+ adagrad, ops::AdagradOpKernel,
+ ops::AdagradOpKernel);
diff --git a/paddle/operators/adagrad_op.h b/paddle/operators/adagrad_op.h
index c5d8f751d3527f89b96d4274328ba0bb5f6efa44..4d4a6434c7c472d8ceb01edfc4050fbb009d6c9f 100644
--- a/paddle/operators/adagrad_op.h
+++ b/paddle/operators/adagrad_op.h
@@ -19,35 +19,59 @@ limitations under the License. */
namespace paddle {
namespace operators {
+template
+struct SparseAdagradFunctor {
+ void operator()(const platform::DeviceContext& context,
+ const framework::SelectedRows& grad,
+ const framework::Tensor& learning_rate, T epsilon,
+ framework::Tensor* moment, framework::Tensor* param);
+};
+
template
class AdagradOpKernel : public framework::OpKernel {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
- auto param_out_tensor = ctx.Output("ParamOut");
- auto moment_out_tensor = ctx.Output("MomentOut");
+ auto* param_out_tensor = ctx.Output("ParamOut");
+ auto* moment_out_tensor = ctx.Output("MomentOut");
param_out_tensor->mutable_data(ctx.GetPlace());
moment_out_tensor->mutable_data(ctx.GetPlace());
- float epsilon = ctx.Attr("epsilon");
-
- auto param = framework::EigenVector::Flatten(
- *ctx.Input("Param"));
- auto grad = framework::EigenVector::Flatten(
- *ctx.Input("Grad"));
- auto moment = framework::EigenVector::Flatten(
- *ctx.Input("Moment"));
- auto lr = framework::EigenVector::Flatten(
- *ctx.Input("LearningRate"));
-
- auto param_out = framework::EigenVector::Flatten(*param_out_tensor);
- auto moment_out = framework::EigenVector::Flatten(*moment_out_tensor);
- auto place = ctx.GetEigenDevice();
-
- 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);
+ T epsilon = static_cast(ctx.Attr("epsilon"));
+
+ auto* grad_var = ctx.InputVar("Grad");
+ if (grad_var->IsType()) {
+ auto param = framework::EigenVector::Flatten(
+ *ctx.Input("Param"));
+ auto grad = framework::EigenVector::Flatten(
+ *ctx.Input("Grad"));
+ auto moment = framework::EigenVector::Flatten(
+ *ctx.Input