-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility to inspect a model's parameters (to get dtype/device)
- Loading branch information
Showing
8 changed files
with
53 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from __future__ import annotations | ||
|
||
import torch.nn | ||
|
||
|
||
def get_param(model) -> torch.nn.Parameter: | ||
""" | ||
Find the first parameter in a model or module. | ||
""" | ||
if hasattr(model, "model") and hasattr(model.model, "parameters"): | ||
# Unpeel a model descriptor to get at the actual Torch module. | ||
model = model.model | ||
|
||
for param in model.parameters(): | ||
return param | ||
|
||
raise ValueError(f"No parameters found in model {model!r}") |
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,19 @@ | ||
import types | ||
|
||
import pytest | ||
import torch | ||
|
||
from modules.torch_utils import get_param | ||
|
||
|
||
@pytest.mark.parametrize("wrapped", [True, False]) | ||
def test_get_param(wrapped): | ||
mod = torch.nn.Linear(1, 1) | ||
cpu = torch.device("cpu") | ||
mod.to(dtype=torch.float16, device=cpu) | ||
if wrapped: | ||
# more or less how spandrel wraps a thing | ||
mod = types.SimpleNamespace(model=mod) | ||
p = get_param(mod) | ||
assert p.dtype == torch.float16 | ||
assert p.device == cpu |