Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions python/paddle/nn/layer/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3060,7 +3060,7 @@ def to(self, *args, **kwargs) -> Self:
dtype=dtype,
blocking=blocking,
include_sublayers=True,
floating_only=False,
floating_only=True,
)

def _apply(
Expand Down Expand Up @@ -3214,8 +3214,14 @@ def _to_impl(
)

def transform(t, device, dtype, blocking):
if floating_only and (not paddle.is_floating_point(t)):
return t
if floating_only and not (
paddle.is_floating_point(t) or paddle.is_complex(t)
):
# Match PyTorch nn.Module.to semantics: skip dtype casting for
# non-floating/complex tensors, but still apply device change.
if device is None:
return t
return self._transform(t, device, None, blocking)
return self._transform(t, device, dtype, blocking)

with warnings.catch_warnings():
Expand Down
20 changes: 10 additions & 10 deletions test/legacy_test/test_api_compatibility_part3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,30 +1337,30 @@ def test_layer_no_args(self):
self.assertIs(ret, linear)
self.assertEqual(linear.weight.dtype, original_dtype)

# ---- Layer.to: all-dtype casting ----
# ---- Layer.to: floating-only dtype casting (PyTorch-aligned) ----

def test_layer_cast_all_with_positional_dtype(self):
"""Layer.to(dtype) casts ALL params and buffers, including int buf."""
def test_layer_cast_floating_only_with_positional_dtype(self):
"""Layer.to(dtype) casts only floating/complex params and buffers."""
model = self._make_model()
self.assertEqual(model.int_buf.dtype, paddle.int32)
model.to(paddle.float64)
self.assertEqual(model.linear.weight.dtype, paddle.float64)
self.assertEqual(model.int_buf.dtype, paddle.float64)
self.assertEqual(model.int_buf.dtype, paddle.int32)

def test_layer_cast_all_with_keyword_dtype(self):
"""Layer.to(dtype='float64') casts ALL params and buffers."""
def test_layer_cast_floating_only_with_keyword_dtype(self):
"""Layer.to(dtype='float64') casts only floating/complex tensors."""
model = self._make_model()
model.to(dtype='float64')
self.assertEqual(model.linear.weight.dtype, paddle.float64)
self.assertEqual(model.int_buf.dtype, paddle.float64)
self.assertEqual(model.int_buf.dtype, paddle.int32)

def test_layer_cast_all_with_tensor(self):
"""Layer.to(tensor) casts ALL params and buffers."""
def test_layer_cast_floating_only_with_tensor(self):
"""Layer.to(tensor) casts only floating/complex tensors to tensor.dtype."""
model = self._make_model()
ref = paddle.to_tensor([1.0], dtype='float64')
model.to(ref)
self.assertEqual(model.linear.weight.dtype, paddle.float64)
self.assertEqual(model.int_buf.dtype, paddle.float64)
self.assertEqual(model.int_buf.dtype, paddle.int32)

# ---- Layer.to: sublayers and chaining ----

Expand Down
Loading