GPTQ_loader.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 llama_inference_offload
  10. import opt
  11. def load_quantized(model_name):
  12. if not shared.args.model_type:
  13. # Try to determine model type from model name
  14. model_type = model_name.split('-')[0].lower()
  15. if model_type not in ('llama', 'opt'):
  16. print("Can't determine model type from model name. Please specify it manually using --gptq-model-type "
  17. "argument")
  18. exit()
  19. else:
  20. model_type = shared.args.model_type.lower()
  21. if model_type == 'llama':
  22. if not shared.args.pre_layer:
  23. load_quant = llama.load_quant
  24. else:
  25. load_quant = llama_inference_offload.load_quant
  26. elif model_type == 'opt':
  27. load_quant = opt.load_quant
  28. else:
  29. print("Unknown pre-quantized model type specified. Only 'llama' and 'opt' are supported")
  30. exit()
  31. path_to_model = Path(f'models/{model_name}')
  32. if path_to_model.name.lower().startswith('llama-7b'):
  33. pt_model = f'llama-7b-{shared.args.wbits}bit.pt'
  34. elif path_to_model.name.lower().startswith('llama-13b'):
  35. pt_model = f'llama-13b-{shared.args.wbits}bit.pt'
  36. elif path_to_model.name.lower().startswith('llama-30b'):
  37. pt_model = f'llama-30b-{shared.args.wbits}bit.pt'
  38. elif path_to_model.name.lower().startswith('llama-65b'):
  39. pt_model = f'llama-65b-{shared.args.wbits}bit.pt'
  40. else:
  41. pt_model = f'{model_name}-{shared.args.wbits}bit.pt'
  42. # Try to find the .pt both in models/ and in the subfolder
  43. pt_path = None
  44. for path in [Path(p) for p in [f"models/{pt_model}", f"{path_to_model}/{pt_model}"]]:
  45. if path.exists():
  46. pt_path = path
  47. if not pt_path:
  48. print(f"Could not find {pt_model}, exiting...")
  49. exit()
  50. # qwopqwop200's offload
  51. if shared.args.pre_layer:
  52. model = load_quant(str(path_to_model), str(pt_path), shared.args.wbits, shared.args.pre_layer)
  53. else:
  54. model = load_quant(str(path_to_model), str(pt_path), shared.args.wbits)
  55. # accelerate offload (doesn't work properly)
  56. if shared.args.gpu_memory:
  57. memory_map = list(map(lambda x : x.strip(), shared.args.gpu_memory))
  58. max_cpu_memory = shared.args.cpu_memory.strip() if shared.args.cpu_memory is not None else '99GiB'
  59. max_memory = {}
  60. for i in range(len(memory_map)):
  61. max_memory[i] = f'{memory_map[i]}GiB' if not re.match('.*ib$', memory_map[i].lower()) else memory_map[i]
  62. max_memory['cpu'] = max_cpu_memory
  63. device_map = accelerate.infer_auto_device_map(model, max_memory=max_memory, no_split_module_classes=["LlamaDecoderLayer"])
  64. print("Using the following device map for the 4-bit model:", device_map)
  65. # https://huggingface.co/docs/accelerate/package_reference/big_modeling#accelerate.dispatch_model
  66. model = accelerate.dispatch_model(model, device_map=device_map, offload_buffers=True)
  67. # No offload
  68. elif not shared.args.cpu:
  69. model = model.to(torch.device('cuda:0'))
  70. return model