-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add get_effective_device(...) utility to aid in determining the effec…
…tive device of models that are partially loaded.
- Loading branch information
Showing
8 changed files
with
39 additions
and
11 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
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
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,20 @@ | ||
import itertools | ||
|
||
import torch | ||
|
||
|
||
def get_effective_device(model: torch.nn.Module) -> torch.device: | ||
"""A utility to infer the 'effective' device of a model. | ||
This utility handles the case where a model is partially loaded onto the GPU, so is safer than just calling: | ||
`next(iter(model.parameters())).device`. | ||
In the worst case, this utility has to check all model parameters, so if you already know the intended model device, | ||
then it is better to avoid calling this function. | ||
""" | ||
# If all parameters are on the CPU, return the CPU device. Otherwise, return the first non-CPU device. | ||
for p in itertools.chain(model.parameters(), model.buffers()): | ||
if p.device.type != "cpu": | ||
return p.device | ||
|
||
return torch.device("cpu") |