aboutsummaryrefslogtreecommitdiffstats
path: root/modules/torch_utils.py
diff options
context:
space:
mode:
authorAarni Koskela <akx@iki.fi>2023-12-30 22:20:30 +0000
committerAarni Koskela <akx@iki.fi>2023-12-31 11:22:43 +0000
commit5768afc776a66bb94e77a9c1daebeea58fa731d5 (patch)
tree06c015a95076b2de579a72750aa7862a9615ed09 /modules/torch_utils.py
parenta84e842189f5599fd354147f72d1a9b9ed0716c8 (diff)
downloadstable-diffusion-webui-gfx803-5768afc776a66bb94e77a9c1daebeea58fa731d5.tar.gz
stable-diffusion-webui-gfx803-5768afc776a66bb94e77a9c1daebeea58fa731d5.tar.bz2
stable-diffusion-webui-gfx803-5768afc776a66bb94e77a9c1daebeea58fa731d5.zip
Add utility to inspect a model's parameters (to get dtype/device)
Diffstat (limited to 'modules/torch_utils.py')
-rw-r--r--modules/torch_utils.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/modules/torch_utils.py b/modules/torch_utils.py
new file mode 100644
index 00000000..e5b52393
--- /dev/null
+++ b/modules/torch_utils.py
@@ -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}")