Skip to content

Commit 3a4b6cd

Browse files
authored
Merge pull request #11488 from luotao1/softmax_doc
add doc of sequence_softmax and parallelDo
2 parents 0ddc5d8 + cff5232 commit 3a4b6cd

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

paddle/fluid/operators/elementwise_mul_op.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ limitations under the License. */
1515
#include "paddle/fluid/operators/elementwise_mul_op.h"
1616
#include "paddle/fluid/operators/elementwise_op.h"
1717
namespace ops = paddle::operators;
18-
REGISTER_ELEMWISE_OP(elementwise_mul, "Mul", "Out = X \\odot\\ Y");
18+
REGISTER_ELEMWISE_OP(elementwise_mul, "Mul", "Out = X \\\\odot Y");
1919
REGISTER_OP_CPU_KERNEL(
2020
elementwise_mul,
2121
ops::ElementwiseMulKernel<paddle::platform::CPUDeviceContext, float>,

python/paddle/fluid/layers/control_flow.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,56 @@ def __exit__(self, exc_type, exc_val, exc_tb):
234234

235235
class ParallelDo(object):
236236
"""
237-
ParallelDo class.
237+
ParallelDo is used to represent multi-thread data parallel processing.
238238
239-
ParallelDo class is used to create a ParallelDo.
239+
Its vanilla implementation can be shown as the following (:math:`|` means
240+
single thread and :math:`||||` means multiple threads)
241+
242+
.. code-block:: text
243+
244+
In the forward pass
245+
| Split input onto different devices
246+
| Copy parameter onto different devices
247+
|||| Compute forward pass in parallel
248+
| Merge output from different devices
249+
250+
In the backward pass
251+
| Split output@grad onto different devices
252+
|||| Compute backward pass in parallel
253+
| accumulate param@grad from different devices to the first device
254+
| Merge input@grad from different devices
255+
| Copy param@grad to the place of parallel_do_op
256+
257+
Examples:
258+
259+
.. code-block:: python
260+
261+
images = fluid.layers.data(name='pixel', shape=[1, 28, 28], dtype=DTYPE)
262+
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
263+
264+
# ParallelDo version & Single-thread version
265+
if thread_num > 1:
266+
places = fluid.layers.get_places(thread_num)
267+
pd = fluid.layers.ParallelDo(places)
268+
with pd.do():
269+
images = pd.read_input(images)
270+
label = pd.read_input(label)
271+
predict = cnn_model(images)
272+
cost = fluid.layers.cross_entropy(input=predict, label=label)
273+
274+
avg_cost = fluid.layers.mean(x=cost)
275+
pd.write_output(avg_cost)
276+
277+
avg_cost = pd()
278+
avg_cost = fluid.layers.mean(avg_cost)
279+
else:
280+
predict = cnn_model(images)
281+
cost = fluid.layers.cross_entropy(input=predict, label=label)
282+
avg_cost = fluid.layers.mean(x=cost)
283+
284+
.. warning::
285+
286+
It will be soon deprecated, please use ParallelExecutor instead.
240287
"""
241288

242289
def __init__(self, places, use_nccl=False, name=None):

python/paddle/fluid/layers/nn.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,41 @@ def sequence_conv(input,
12101210

12111211

12121212
def sequence_softmax(input, param_attr=None, bias_attr=None, use_cudnn=True):
1213+
"""
1214+
This function computes the softmax activation among all time-steps for each
1215+
sequence. The dimension of each time-step should be 1. Thus, the shape of
1216+
input Tensor can be either :math:`[N, 1]` or :math:`[N]`, where :math:`N`
1217+
is the sum of the length of all sequences.
1218+
1219+
For i-th sequence in a mini-batch:
1220+
1221+
.. math::
1222+
1223+
Out(X[lod[i]:lod[i+1]], :) = \\frac{\exp(X[lod[i]:lod[i+1], :])}{\sum(\exp(X[lod[i]:lod[i+1], :]))}
1224+
1225+
For example, for a mini-batch of 3 sequences with variable-length,
1226+
each containing 2, 3, 2 time-steps, the lod of which is [0, 2, 5, 7],
1227+
then softmax will be computed among :math:`X[0:2, :]`, :math:`X[2:5, :]`,
1228+
:math:`X[5:7, :]`, and :math:`N` turns out to be 7.
1229+
1230+
Args:
1231+
input (Variable): The input variable which is a LoDTensor.
1232+
bias_attr (ParamAttr|None): attributes for bias
1233+
param_attr (ParamAttr|None): attributes for parameter
1234+
use_cudnn (bool): Use cudnn kernel or not, it is valid only when the cudnn \
1235+
library is installed. Default: True
1236+
1237+
Returns:
1238+
Variable: output of sequence_softmax
1239+
1240+
Examples:
1241+
1242+
.. code-block:: python
1243+
1244+
x = fluid.layers.data(name='x', shape=[7, 1],
1245+
dtype='float32', lod_level=1)
1246+
x_sequence_softmax = fluid.layers.sequence_softmax(input=x)
1247+
"""
12131248
helper = LayerHelper('sequence_softmax', **locals())
12141249
dtype = helper.input_dtype()
12151250
softmax_out = helper.create_tmp_variable(dtype)

0 commit comments

Comments
 (0)