From 4621f82fe94942f4458173b6dde7d42b5d93a9b6 Mon Sep 17 00:00:00 2001 From: feifei-111 Date: Thu, 22 Sep 2022 11:07:18 +0800 Subject: [PATCH] [API] added 'place' dummy interface for variable (#45877) * place * add test * fix spell and test * fix anno * code style --- python/paddle/fluid/layers/math_op_patch.py | 25 +++++++++++---- .../unittests/dygraph_to_static/test_place.py | 32 +++++++++++++++++++ 2 files changed, 51 insertions(+), 6 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 1afea145e4a..4bb6931c8a4 100644 --- a/python/paddle/fluid/layers/math_op_patch.py +++ b/python/paddle/fluid/layers/math_op_patch.py @@ -132,21 +132,33 @@ 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