You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Conditional block will automatically try to find the variables in its parent blocks. However, if there are nested blocks, some variables may reside in its parent's parent block. In that case, the conditional block will not be able to find them and cause an error.
Here is a minimal environment to reproduce the issue:
import unittest
import contextlib
import paddle.fluid.core as core
import paddle.fluid.layers as layers
import paddle.fluid.framework as framework
from paddle.fluid.executor import Executor
from paddle.fluid.framework import default_startup_program
class TestHelper(object):
def __init__(self):
self._counter = layers.zeros(shape=[1], dtype='int64')
self._max_len = layers.fill_constant(
shape=[1], dtype='int64', value=10)
self._cond = layers.less_than(
x=self._counter,
y=layers.fill_constant(
shape=[1], dtype='int64', value=10))
self._while_op = layers.While(self._cond)
@contextlib.contextmanager
def block(self):
with self._while_op.block():
yield
# A nested block structure to reproduce the issue.
# Variable fill_constant cannot be found in parent block,
# but could be found in parent's parent block.
# Conditional block cannot handle this and will raise an error.
with layers.Switch() as switch:
with switch.case(self._cond):
layers.increment(x=self._counter, value=1.0, in_place=True)
layers.less_than(
x=self._counter, y=self._max_len, cond=self._cond)
class TestNested(unittest.TestCase):
def setUp(self):
helper = TestHelper()
var = layers.fill_constant(
shape=[1], dtype='int64', value=7)
with helper.block():
layers.Print(var,
message="The test is to print this var each step")
def testNested(self):
cpu = core.CPUPlace()
exe = Executor(cpu)
exe.run(default_startup_program())
exe.run(framework.default_main_program())
if __name__ == '__main__':
unittest.main()
The text was updated successfully, but these errors were encountered:
Conditional block will automatically try to find the variables in its parent blocks. However, if there are nested blocks, some variables may reside in its parent's parent block. In that case, the conditional block will not be able to find them and cause an error.
Here is a minimal environment to reproduce the issue:
The text was updated successfully, but these errors were encountered: