evaluation.py 572 B

1234567891011121314151617
  1. def model_size(model, param_size=32):
  2. """Calculate the model parameter sizes, including non-trainable parameters
  3. Args:
  4. model (nn.Module): A PyTorch model.
  5. param_size (int): The size of a parameter, default using float32.
  6. Returns:
  7. float: The model size in MB.
  8. """
  9. # sum(p.numel() for p in model.parameters() if p.requires_grad) for only trainable parameters
  10. params = sum(p.numel() for p in model.parameters())
  11. return bit_to_megabyte(params * param_size)
  12. def bit_to_megabyte(bits):
  13. return bits / (8 * 1024 * 1024)