From a1a2054e97fe146d22e6b454c0b30ee0a42100c3 Mon Sep 17 00:00:00 2001 From: zhangbo9674 <82555433+zhangbo9674@users.noreply.github.com> Date: Tue, 21 Jun 2022 14:59:09 +0800 Subject: [PATCH] [FIx bug]layer to 'NoneType' object has no attribute 'place' (#43597) * refine layer to * refine code * add ut --- python/paddle/fluid/dygraph/layers.py | 3 ++- python/paddle/fluid/tests/unittests/test_base_layer.py | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/dygraph/layers.py b/python/paddle/fluid/dygraph/layers.py index 168b894d563..490c6a1ca76 100644 --- a/python/paddle/fluid/dygraph/layers.py +++ b/python/paddle/fluid/dygraph/layers.py @@ -1594,7 +1594,8 @@ class Layer(object): blocking) for key, buf in self._buffers.items(): - self._buffers[key] = func(buf, device, dtype, blocking) + if buf is not None: + self._buffers[key] = func(buf, device, dtype, blocking) self._dtype = dtype diff --git a/python/paddle/fluid/tests/unittests/test_base_layer.py b/python/paddle/fluid/tests/unittests/test_base_layer.py index bb5c691a6e0..dbfb1844fb0 100644 --- a/python/paddle/fluid/tests/unittests/test_base_layer.py +++ b/python/paddle/fluid/tests/unittests/test_base_layer.py @@ -559,16 +559,25 @@ class TestLayerTo(unittest.TestCase): else: self.assertTrue(isinstance(p, paddle.fluid.framework.ParamBase)) + def func_test_to_api_none_buffer(self): + model = paddle.nn.Linear(2, 4) + buffer = None + model.register_buffer("buf_name", buffer, persistable=True) + model.to(dtype='float64') + self.assertEqual(model._buffers['buf_name'], None) + def test_main(self): with _test_eager_guard(): self.funcsetUp() self.func_test_to_api() self.func_test_to_api_paddle_dtype() self.func_test_to_api_numpy_dtype() + self.func_test_to_api_none_buffer() self.funcsetUp() self.func_test_to_api() self.func_test_to_api_paddle_dtype() self.func_test_to_api_numpy_dtype() + self.func_test_to_api_none_buffer() if __name__ == '__main__': -- GitLab