Skip to content

Commit 810dc8d

Browse files
Dpakkkclaude
andcommitted
#36 - Fix to_device() to preserve integer tensor dtype
Only apply dtype conversion to floating-point tensors. Integer tensors (e.g., input_ids, attention_mask) must preserve their dtype for compatibility with Hugging Face models during mixed-precision inference. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Signed-off-by: Dpakkk <pokharelbikash981@gmail.com>
1 parent e0e2ac3 commit 810dc8d

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/alpamayo_r1/helper.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ def to_device(
8686
) -> Any:
8787
"""Recursively cast data into the specified device, dtype."""
8888
if isinstance(data, torch.Tensor):
89-
data = data.to(
90-
device=device,
91-
dtype=dtype,
92-
)
93-
return data
89+
# Only apply dtype conversion to floating-point tensors.
90+
# Integer tensors (e.g., input_ids, attention_mask) must preserve their dtype
91+
# for compatibility with Hugging Face models during mixed-precision inference.
92+
if dtype is not None and data.is_floating_point():
93+
return data.to(device=device, dtype=dtype)
94+
return data.to(device=device)
9495
elif isinstance(data, collections.abc.Mapping):
9596
return {key: to_device(data[key], device=device, dtype=dtype) for key in data}
9697
elif isinstance(data, collections.abc.Sequence) and not isinstance(data, (str, bytes)):

0 commit comments

Comments
 (0)