Skip to content

Commit

Permalink
[FIx bug]layer to 'NoneType' object has no attribute 'place' (#43597) (
Browse files Browse the repository at this point in the history
…#43717)

bug:
当class Layer的_buffers中有参数为None的时候,调用to()方法将会报layer to 'NoneType' object has no attribute 'place'的错误。
修复方法:
to()方法增加对_buffers中None类型参数的判断,如果为None,跳过该参数的处理。
  • Loading branch information
zhangbo9674 authored Jun 22, 2022
1 parent af415bc commit 0b87931
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/paddle/fluid/dygraph/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,8 @@ def _apply(self, func, device, dtype, blocking, include_sublayers=True):
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

Expand Down
9 changes: 9 additions & 0 deletions python/paddle/fluid/tests/unittests/test_base_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,16 +544,25 @@ def func_test_to_api_numpy_dtype(self):
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__':
Expand Down

0 comments on commit 0b87931

Please sign in to comment.