From 4a8aa6d89db182bba60cd5bdf40e9d3f6381ad26 Mon Sep 17 00:00:00 2001 From: feifei-111 Date: Mon, 26 Sep 2022 11:54:37 +0800 Subject: [PATCH] fix conflict (#46388) --- python/paddle/fluid/layers/math_op_patch.py | 29 ++++++++++++----- .../unittests/dygraph_to_static/test_place.py | 32 +++++++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 python/paddle/fluid/tests/unittests/dygraph_to_static/test_place.py diff --git a/python/paddle/fluid/layers/math_op_patch.py b/python/paddle/fluid/layers/math_op_patch.py index 7c66e9736ea..d804b25db60 100644 --- a/python/paddle/fluid/layers/math_op_patch.py +++ b/python/paddle/fluid/layers/math_op_patch.py @@ -131,22 +131,34 @@ def monkey_patch_variable(): @static_only def cpu(self): - """ - Variable should not have cpu() and cuda() interface. - But this interface can greatly facilitate dy2static. - We do nothing here. + """ + Variable should not have cpu() and cuda() interface. + But this interface can greatly facilitate dy2static. + We do nothing here. """ return self @static_only def cuda(self): - """ - Variable should not have cpu() and cuda() interface. - But this interface can greatly facilitate dy2static. - We do nothing here. + """ + Variable should not have cpu() and cuda() interface. + But this interface can greatly facilitate dy2static. + We do nothing here. """ return self + @static_only + def place(self): + """ + Variable don't have 'place' interface in static mode + But this interface can greatly facilitate dy2static. + So we give a warnning here and return None. + """ + warnings.warn( + "Variable do not have 'place' interface for static mode, try not to use it. None will be returned." + ) + return None + def astype(self, dtype): """ **Notes**: @@ -427,6 +439,7 @@ def monkey_patch_variable(): ('astype', astype), ('cpu', cpu), ('cuda', cuda), + ('place', place), ('append', append), ('item', _item), ('pop', pop), diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_place.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_place.py new file mode 100644 index 00000000000..7e177ee0c0c --- /dev/null +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_place.py @@ -0,0 +1,32 @@ +# Copyright (c) 2021 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. + +from __future__ import print_function + +import numpy as np +import paddle +import unittest + + +class TestPlace(unittest.TestCase): + + def test_place(self): + + paddle.enable_static() + x = paddle.to_tensor([1, 2, 3, 4]) + self.assertTrue(x.place() == None) + + +if __name__ == '__main__': + unittest.main() -- GitLab