convert-to-torch.py 763 B

1234567891011121314151617181920212223242526
  1. '''
  2. Converts a transformers model to .pt, which is faster to load.
  3. Example:
  4. python convert-to-torch.py models/opt-1.3b
  5. The output will be written to torch-dumps/name-of-the-model.pt
  6. '''
  7. from transformers import AutoModelForCausalLM, T5ForConditionalGeneration
  8. import torch
  9. from sys import argv
  10. from pathlib import Path
  11. path = Path(argv[1])
  12. model_name = path.name
  13. print(f"Loading {model_name}...")
  14. if model_name in ['flan-t5', 't5-large']:
  15. model = T5ForConditionalGeneration.from_pretrained(path).cuda()
  16. else:
  17. model = AutoModelForCausalLM.from_pretrained(path, low_cpu_mem_usage=True, torch_dtype=torch.float16).cuda()
  18. print("Model loaded.")
  19. print(f"Saving to torch-dumps/{model_name}.pt")
  20. torch.save(model, Path(f"torch-dumps/{model_name}.pt"))