2023-12-30 15:20:30 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import torch.nn
|
2024-05-16 08:16:50 -06:00
|
|
|
import torch
|
2023-12-30 15:20:30 -07:00
|
|
|
|
|
|
|
|
|
|
|
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}")
|
2024-05-16 08:16:50 -06:00
|
|
|
|
|
|
|
|
|
|
|
def float64(t: torch.Tensor):
|
|
|
|
"""return torch.float64 if device is not mps or xpu, else return torch.float32"""
|
2024-06-25 01:24:46 -06:00
|
|
|
if t.device.type in ['mps', 'xpu']:
|
|
|
|
return torch.float32
|
2024-05-16 08:16:50 -06:00
|
|
|
return torch.float64
|