From 2440a8e1f057ce1c9b94ba3671064d6e688b23f2 Mon Sep 17 00:00:00 2001 From: zhhsplendid Date: Tue, 29 Sep 2020 06:09:03 +0000 Subject: [PATCH 1/5] Refine cond API doc for 2.0rc, test=document_fix --- python/paddle/fluid/layers/control_flow.py | 61 ++++++++++++---------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index b6a1e3b151bde..9c1abc68be031 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -2324,16 +2324,18 @@ def cond(pred, true_fn=None, false_fn=None, name=None): .. code-block:: python - import paddle.fluid as fluid - a = fluid.data(name='a', shape=[-1, 1], dtype='float32') - b = fluid.data(name='b', shape=[-1, 1], dtype='float32') + import paddle + + paddle.enable_static() + a = paddle.data(name='a', shape=[-1, 1], dtype='float32') + b = paddle.data(name='b', shape=[-1, 1], dtype='float32') c = a * b - out = fluid.layers.cond(a < b, lambda: a + c, lambda: b * b) + out = paddle.nn.cond(a < b, lambda: a + c, lambda: b * b) No matter whether ``a < b`` , ``c = a * b`` will run. Args: - pred(Variable): A boolean tensor whose numel should be 1. The boolean + pred(Tensor): A boolean tensor whose numel should be 1. The boolean value determines whether to return the result of ``true_fn`` or ``false_fn`` . true_fn(callable, optional): A callable to be performed if ``pred`` is @@ -2345,7 +2347,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None): refer to :ref:`api_guide_Name` . Returns: - Variable|list(Variable)|tuple(Variable): returns ``true_fn()`` if the + Tensor|list(Tensor)|tuple(Tensor): returns ``true_fn()`` if the predicate ``pred`` is true else ``false_fn()`` . Raises: @@ -2356,10 +2358,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None): Examples: .. code-block:: python - import paddle.fluid as fluid - import paddle.fluid.layers as layers - from paddle.fluid.executor import Executor - from paddle.fluid.framework import Program, program_guard + import paddle # # pseudocode: @@ -2369,32 +2368,38 @@ def cond(pred, true_fn=None, false_fn=None, name=None): # return 3, 2 # + def true_func(): - return layers.fill_constant( - shape=[1, 2], dtype='int32', value=1), layers.fill_constant( - shape=[2, 3], dtype='bool', value=True) + return paddle.fill_constant(shape=[1, 2], dtype='int32', + value=1), paddle.fill_constant(shape=[2, 3], + dtype='bool', + value=True) + def false_func(): - return layers.fill_constant( - shape=[3, 4], dtype='float32', value=3), layers.fill_constant( - shape=[4, 5], dtype='int64', value=2) - - main_program = Program() - startup_program = Program() - with program_guard(main_program, startup_program): - x = layers.fill_constant(shape=[1], dtype='float32', value=0.1) - y = layers.fill_constant(shape=[1], dtype='float32', value=0.23) - pred = layers.less_than(x, y) - out = layers.cond(pred, true_func, false_func) + return paddle.fill_constant(shape=[3, 4], dtype='float32', + value=3), paddle.fill_constant(shape=[4, 5], + dtype='int64', + value=2) + + paddle.enable_static() + + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + with paddle.static.program_guard(main_program, startup_program): + x = paddle.fill_constant(shape=[1], dtype='float32', value=0.1) + y = paddle.fill_constant(shape=[1], dtype='float32', value=0.23) + pred = paddle.less_than(x=x, y=y, name=None) + out = paddle.nn.cond(pred, true_func, false_func) # out is a tuple containing 2 tensors - place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda( - ) else fluid.CPUPlace() - exe = fluid.Executor(place) + place = paddle.CUDAPlace( + 0) if paddle.fluid.core.is_compiled_with_cuda() else paddle.CPUPlace() + exe = paddle.static.Executor(place) ret = exe.run(main_program, fetch_list=out) # ret[0] = [[1 1]] # ret[1] = [[ True True True] - # [ True True True]] + # [ True True True]] """ if in_dygraph_mode(): From c02659899dfe26eea670ee84b23b40e0dbaabc5b Mon Sep 17 00:00:00 2001 From: zhhsplendid Date: Tue, 29 Sep 2020 08:09:23 +0000 Subject: [PATCH 2/5] Fix is_compiled_with_cude, test=develop --- python/paddle/fluid/layers/control_flow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index 9c1abc68be031..68787d177f227 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -2394,7 +2394,7 @@ def false_func(): # out is a tuple containing 2 tensors place = paddle.CUDAPlace( - 0) if paddle.fluid.core.is_compiled_with_cuda() else paddle.CPUPlace() + 0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace() exe = paddle.static.Executor(place) ret = exe.run(main_program, fetch_list=out) # ret[0] = [[1 1]] From d5bc320ca75f0df9b7b5efed3a10250451a2d286 Mon Sep 17 00:00:00 2001 From: zhhsplendid Date: Tue, 29 Sep 2020 09:57:32 +0000 Subject: [PATCH 3/5] Change paddle.data to paddle.static.data, test=document_fix --- python/paddle/fluid/layers/control_flow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index 68787d177f227..a363a19c6dd30 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -2327,8 +2327,8 @@ def cond(pred, true_fn=None, false_fn=None, name=None): import paddle paddle.enable_static() - a = paddle.data(name='a', shape=[-1, 1], dtype='float32') - b = paddle.data(name='b', shape=[-1, 1], dtype='float32') + a = paddle.static.data(name='a', shape=[-1, 1], dtype='float32') + b = paddle.static.data(name='b', shape=[-1, 1], dtype='float32') c = a * b out = paddle.nn.cond(a < b, lambda: a + c, lambda: b * b) From 8c0663774fcd78bdee271a52de16a555bff061dc Mon Sep 17 00:00:00 2001 From: zhhsplendid Date: Thu, 8 Oct 2020 10:40:22 +0000 Subject: [PATCH 4/5] Modify the doc to dygraph, test=document_fix --- python/paddle/fluid/layers/control_flow.py | 28 +++++++--------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index a363a19c6dd30..02046f1fe4431 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -2323,12 +2323,12 @@ def cond(pred, true_fn=None, false_fn=None, name=None): semantics. For example: .. code-block:: python - + + import numpy as np import paddle - paddle.enable_static() - a = paddle.static.data(name='a', shape=[-1, 1], dtype='float32') - b = paddle.static.data(name='b', shape=[-1, 1], dtype='float32') + a = paddle.to_tensor(np.zeros((1, 1))) + b = paddle.to_tensor(np.zeros((1, 1))) c = a * b out = paddle.nn.cond(a < b, lambda: a + c, lambda: b * b) @@ -2382,21 +2382,11 @@ def false_func(): dtype='int64', value=2) - paddle.enable_static() - - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - x = paddle.fill_constant(shape=[1], dtype='float32', value=0.1) - y = paddle.fill_constant(shape=[1], dtype='float32', value=0.23) - pred = paddle.less_than(x=x, y=y, name=None) - out = paddle.nn.cond(pred, true_func, false_func) - # out is a tuple containing 2 tensors - - place = paddle.CUDAPlace( - 0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace() - exe = paddle.static.Executor(place) - ret = exe.run(main_program, fetch_list=out) + x = paddle.fill_constant(shape=[1], dtype='float32', value=0.1) + y = paddle.fill_constant(shape=[1], dtype='float32', value=0.23) + pred = paddle.less_than(x=x, y=y, name=None) + ret = paddle.nn.cond(pred, true_func, false_func) + # ret is a tuple containing 2 tensors # ret[0] = [[1 1]] # ret[1] = [[ True True True] # [ True True True]] From 5c9c418b0bb58ca557618e1642778f06c29eec90 Mon Sep 17 00:00:00 2001 From: zhhsplendid Date: Mon, 12 Oct 2020 06:17:45 +0000 Subject: [PATCH 5/5] Modify based on reviewer's comment, test=document_fix --- python/paddle/fluid/layers/control_flow.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index 02046f1fe4431..130b5f6c89d29 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -2297,11 +2297,6 @@ def copy_var_to_parent_block(var, layer_helper): def cond(pred, true_fn=None, false_fn=None, name=None): """ - :api_attr: Static Graph - :alias_main: paddle.nn.cond - :alias: paddle.nn.cond,paddle.nn.control_flow.cond - :old_api: paddle.fluid.layers.cond - This API returns ``true_fn()`` if the predicate ``pred`` is true else ``false_fn()`` . Users could also set ``true_fn`` or ``false_fn`` to ``None`` if do nothing and this API will treat the callable simply returns @@ -2324,11 +2319,10 @@ def cond(pred, true_fn=None, false_fn=None, name=None): .. code-block:: python - import numpy as np import paddle - a = paddle.to_tensor(np.zeros((1, 1))) - b = paddle.to_tensor(np.zeros((1, 1))) + a = paddle.zeros((1, 1)) + b = paddle.zeros((1, 1)) c = a * b out = paddle.nn.cond(a < b, lambda: a + c, lambda: b * b)