-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
49 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from torchdata.utils.pin_memory import pin_memory_fn | ||
|
||
__all__ = ["pin_memory_fn"] | ||
|
||
assert __all__ == sorted(__all__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import collections | ||
|
||
import torch | ||
|
||
|
||
def pin_memory_fn(data, device=None): | ||
if hasattr(data, "pin_memory"): | ||
return data.pin_memory(device) | ||
elif isinstance(data, torch.Tensor): | ||
return data.pin_memory(device) | ||
elif isinstance(data, (str, bytes)): | ||
return data | ||
elif isinstance(data, collections.abc.Mapping): | ||
pinned_data = {k: pin_memory_fn(sample, device) for k, sample in data.items()} | ||
try: | ||
return type(data)(**pinned_data) | ||
except TypeError: | ||
# The mapping type may not support `__init__(iterable)`. | ||
return pinned_data | ||
elif isinstance(data, collections.abc.Sequence): | ||
pinned_data = [pin_memory_fn(sample, device) for sample in data] # type: ignore[assignment] | ||
try: | ||
type(data)(*pinned_data) | ||
except TypeError: | ||
# The sequence type may not support `__init__(iterable)` (e.g., `range`). | ||
return pinned_data | ||
else: | ||
return data |