torch_utils.py 700 B

1234567891011121314151617181920212223242526
  1. from __future__ import annotations
  2. import torch.nn
  3. import torch
  4. def get_param(model) -> torch.nn.Parameter:
  5. """
  6. Find the first parameter in a model or module.
  7. """
  8. if hasattr(model, "model") and hasattr(model.model, "parameters"):
  9. # Unpeel a model descriptor to get at the actual Torch module.
  10. model = model.model
  11. for param in model.parameters():
  12. return param
  13. raise ValueError(f"No parameters found in model {model!r}")
  14. def float64(t: torch.Tensor):
  15. """return torch.float64 if device is not mps or xpu, else return torch.float32"""
  16. match t.device.type:
  17. case 'mps', 'xpu':
  18. return torch.float32
  19. return torch.float64