diff --git a/python/paddle/fluid/layers/math_op_patch.py b/python/paddle/fluid/layers/math_op_patch.py index 7c66e9736ea89daa83b8c892605ffec9788ce2c6..d804b25db607473b4f6d60e0f2453038d5ec5f00 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 0000000000000000000000000000000000000000..7e177ee0c0cc01597c4eb7490cc692a81651c6fc --- /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()