Skip to content
Open
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
14 changes: 8 additions & 6 deletions src/alpamayo_r1/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ def to_device(
device: str | torch.device | None = None,
dtype: torch.dtype | None = None,
) -> Any:
"""Recursively cast data into the specified device, dtype."""
"""Recursively cast data into the specified device, dtype.

Note: dtype conversion is only applied to floating-point tensors.
Integer and boolean tensors preserve their original dtype.
"""
if isinstance(data, torch.Tensor):
data = data.to(
device=device,
dtype=dtype,
)
return data
if dtype is not None and data.is_floating_point():
return data.to(device=device, dtype=dtype)
return data.to(device=device)
elif isinstance(data, collections.abc.Mapping):
return {key: to_device(data[key], device=device, dtype=dtype) for key in data}
elif isinstance(data, collections.abc.Sequence) and not isinstance(data, (str, bytes)):
Expand Down
Loading