GPTQ_loader.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import re
  2. import sys
  3. from pathlib import Path
  4. import accelerate
  5. import torch
  6. import modules.shared as shared
  7. sys.path.insert(0, str(Path("repositories/GPTQ-for-LLaMa")))
  8. import llama
  9. import opt
  10. def load_quantized(model_name):
  11. if not shared.args.gptq_model_type:
  12. # Try to determine model type from model name
  13. model_type = model_name.split('-')[0].lower()
  14. if model_type not in ('llama', 'opt'):
  15. print("Can't determine model type from model name. Please specify it manually using --gptq-model-type "
  16. "argument")
  17. exit()
  18. else:
  19. model_type = shared.args.gptq_model_type.lower()
  20. if model_type == 'llama':
  21. load_quant = llama.load_quant
  22. elif model_type == 'opt':
  23. load_quant = opt.load_quant
  24. else:
  25. print("Unknown pre-quantized model type specified. Only 'llama' and 'opt' are supported")
  26. exit()
  27. path_to_model = Path(f'models/{model_name}')
  28. if path_to_model.name.lower().startswith('llama-7b'):
  29. pt_model = f'llama-7b-{shared.args.gptq_bits}bit.pt'
  30. elif path_to_model.name.lower().startswith('llama-13b'):
  31. pt_model = f'llama-13b-{shared.args.gptq_bits}bit.pt'
  32. elif path_to_model.name.lower().startswith('llama-30b'):
  33. pt_model = f'llama-30b-{shared.args.gptq_bits}bit.pt'
  34. elif path_to_model.name.lower().startswith('llama-65b'):
  35. pt_model = f'llama-65b-{shared.args.gptq_bits}bit.pt'
  36. else:
  37. pt_model = f'{model_name}-{shared.args.gptq_bits}bit.pt'
  38. # Try to find the .pt both in models/ and in the subfolder
  39. pt_path = None
  40. for path in [Path(p) for p in [f"models/{pt_model}", f"{path_to_model}/{pt_model}"]]:
  41. if path.exists():
  42. pt_path = path
  43. if not pt_path:
  44. print(f"Could not find {pt_model}, exiting...")
  45. exit()
  46. model = load_quant(str(path_to_model), str(pt_path), shared.args.gptq_bits)
  47. # Multiple GPUs or GPU+CPU
  48. if shared.args.gpu_memory:
  49. memory_map = list(map(lambda x : x.strip(), shared.args.gpu_memory))
  50. max_cpu_memory = shared.args.cpu_memory.strip() if shared.args.cpu_memory is not None else '99GiB'
  51. max_memory = {}
  52. for i in range(len(memory_map)):
  53. max_memory[i] = f'{memory_map[i]}GiB' if not re.match('.*ib$', memory_map[i].lower()) else memory_map[i]
  54. max_memory['cpu'] = max_cpu_memory
  55. device_map = accelerate.infer_auto_device_map(model, max_memory=max_memory, no_split_module_classes=["LlamaDecoderLayer"])
  56. print("Using the following device map for the 4-bit model:", device_map)
  57. # https://huggingface.co/docs/accelerate/package_reference/big_modeling#accelerate.dispatch_model
  58. model = accelerate.dispatch_model(model, device_map=device_map, offload_buffers=True)
  59. # Single GPU
  60. elif not shared.args.cpu:
  61. model = model.to(torch.device('cuda:0'))
  62. return model