Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def ConvertBatchToNumpy(self, batch) -> np.ndarray:

return return_data

def ConvertBatchToPyTorch(self, batch: Any) -> torch.Tensor:
def ConvertBatchToPyTorch(self, batch: Any, device=None) -> torch.Tensor:
"""Convert a RTensor into a PyTorch tensor

Args:
Expand All @@ -404,7 +404,7 @@ def ConvertBatchToPyTorch(self, batch: Any) -> torch.Tensor:

data.reshape((batch_size * num_columns,))

return_data = torch.as_tensor(np.asarray(data)).reshape(batch_size, num_columns)
return_data = torch.as_tensor(np.asarray(data), device=device).reshape(batch_size, num_columns)

# Splice target column from the data if target is given
if self.target_given:
Expand Down Expand Up @@ -704,12 +704,16 @@ def as_numpy(self) -> FormattedLoader:
self._ensure_created()
return FormattedLoader(self._internal, self._internal.ConvertBatchToNumpy, self._is_training)

def as_torch(self) -> FormattedLoader:
def as_torch(self, device: str | torch.device | None = None) -> FormattedLoader:
"""
Return an iterable that yields batches as PyTorch tensors.

Args:
device: If given, the returned tensors are moved to the specified device.
"""
self._ensure_created()
return FormattedLoader(self._internal, self._internal.ConvertBatchToPyTorch, self._is_training)
conversion_fn = lambda batch: self._internal.ConvertBatchToPyTorch(batch, device) # noqa: E731
return FormattedLoader(self._internal, conversion_fn, self._is_training)

def as_tensorflow(self) -> tf.data.Dataset:
"""
Expand Down
12 changes: 6 additions & 6 deletions bindings/pyroot/pythonizations/test/ml_dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ def test12_PyTorch(self):
collected_z_train = []
collected_z_val = []

iter_train = iter(gen_train.as_torch())
iter_train = iter(gen_train.as_torch(device="cpu"))
iter_val = iter(gen_validation.as_torch())

for _ in range(self.n_train_batch):
Expand Down Expand Up @@ -1792,7 +1792,7 @@ def test12_PyTorch(self):
collected_z_val = []

iter_train = iter(gen_train.as_torch())
iter_val = iter(gen_validation.as_torch())
iter_val = iter(gen_validation.as_torch(device="cpu"))

for _ in range(self.n_train_batch):
x, y, z = next(iter_train)
Expand Down Expand Up @@ -2984,7 +2984,7 @@ def test12_PyTorch(self):
collected_z_train = []
collected_z_val = []

iter_train = iter(gen_train.as_torch())
iter_train = iter(gen_train.as_torch(device="cpu"))
iter_val = iter(gen_validation.as_torch())

for _ in range(self.n_train_batch):
Expand Down Expand Up @@ -4133,7 +4133,7 @@ def test12_PyTorch(self):
collected_z_val = []

iter_train = iter(gen_train.as_torch())
iter_val = iter(gen_validation.as_torch())
iter_val = iter(gen_validation.as_torch(device="cpu"))

for _ in range(self.n_train_batch):
x, y, z = next(iter_train)
Expand Down Expand Up @@ -5546,7 +5546,7 @@ def test12_PyTorch(self):
collected_z_train = []
collected_z_val = []

iter_train = iter(gen_train.as_torch())
iter_train = iter(gen_train.as_torch(device="cpu"))
iter_val = iter(gen_validation.as_torch())

for _ in range(self.n_train_batch):
Expand Down Expand Up @@ -7086,7 +7086,7 @@ def test12_PyTorch(self):
collected_z_val = []

iter_train = iter(gen_train.as_torch())
iter_val = iter(gen_validation.as_torch())
iter_val = iter(gen_validation.as_torch(device="cpu"))

for _ in range(self.n_train_batch):
x, y, z = next(iter_train)
Expand Down
Loading