Skip to content

[Dy2St] fix name conflict between mulitple to_static program #52692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python/paddle/jit/dy2static/partial_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,12 +940,19 @@ def _restore_out(self, out_vars):
flatten_outputs = self._outputs.tolist()
for i, idx in enumerate(self._outputs.var_ids):
flatten_outputs[idx] = out_vars[i]
self._clear_outputs_name(flatten_outputs)
outs = self._outputs.restore(flatten_outputs)
if outs is not None and len(outs) == 1:
outs = outs[0]

return outs

def _clear_outputs_name(self, outputs):
"""Clear output name to avoid name conflict in mulitple program"""
for output in outputs:
if isinstance(output, paddle.Tensor):
output.name = ""

@switch_to_static_graph
def _clone_for_test(self, main_program):
return main_program.clone(for_test=True)
Expand Down
58 changes: 58 additions & 0 deletions test/dygraph_to_static/test_multiple_to_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

import unittest

import numpy as np

import paddle
from paddle.jit import to_static


@paddle.jit.not_to_static
def part1(x):
return x + 1


@paddle.jit.not_to_static
def part2(x):
return x + x


def foo(x):
x = to_static(part1)(x)

# It will enter a new unique_name guard, so before applying this fix (#52692),
# the name of x will be conflict with the name of x in part2 (they are
# both `tmp_0`)
paddle.enable_static()
paddle.disable_static()

x = to_static(part2)(x)
return x


class TestMultipleToStaticNameConflict(unittest.TestCase):
def test_multiple_to_static(self):
x = paddle.to_tensor([4.0])

paddle.jit.enable_to_static(False)
out_dygraph = foo(x)
paddle.jit.enable_to_static(True)
out_static = foo(x)
np.testing.assert_allclose(out_dygraph.numpy(), out_static.numpy())


if __name__ == "__main__":
unittest.main()