GPTQ_loader.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.gptq_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.gptq_model_type.lower()
  21. if model_type == 'llama':
  22. if not shared.args.gptq_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.gptq_bits}bit'
  34. elif path_to_model.name.lower().startswith('llama-13b'):
  35. pt_model = f'llama-13b-{shared.args.gptq_bits}bit'
  36. elif path_to_model.name.lower().startswith('llama-30b'):
  37. pt_model = f'llama-30b-{shared.args.gptq_bits}bit'
  38. elif path_to_model.name.lower().startswith('llama-65b'):
  39. pt_model = f'llama-65b-{shared.args.gptq_bits}bit'
  40. else:
  41. pt_model = f'{model_name}-{shared.args.gptq_bits}bit'
  42. # Try to find the .safetensors or .pt both in models/ and in the subfolder
  43. pt_path = None
  44. for path in [Path(p+ext) for ext in ['.safetensors', '.pt'] for p in [f"models/{pt_model}", f"{path_to_model}/{pt_model}"]]:
  45. if path.exists():
  46. print(f"Found {path}")
  47. pt_path = path
  48. break
  49. if not pt_path:
  50. print(f"Could not find {pt_model}, exiting...")
  51. exit()
  52. # qwopqwop200's offload
  53. if shared.args.gptq_pre_layer:
  54. model = load_quant(str(path_to_model), str(pt_path), shared.args.gptq_bits, shared.args.gptq_pre_layer)
  55. else:
  56. model = load_quant(str(path_to_model), str(pt_path), shared.args.gptq_bits)
  57. # accelerate offload (doesn't work properly)
  58. if shared.args.gpu_memory:
  59. memory_map = list(map(lambda x : x.strip(), shared.args.gpu_memory))
  60. max_cpu_memory = shared.args.cpu_memory.strip() if shared.args.cpu_memory is not None else '99GiB'
  61. max_memory = {}
  62. for i in range(len(memory_map)):
  63. max_memory[i] = f'{memory_map[i]}GiB' if not re.match('.*ib$', memory_map[i].lower()) else memory_map[i]
  64. max_memory['cpu'] = max_cpu_memory
  65. device_map = accelerate.infer_auto_device_map(model, max_memory=max_memory, no_split_module_classes=["LlamaDecoderLayer"])
  66. print("Using the following device map for the 4-bit model:", device_map)
  67. # https://huggingface.co/docs/accelerate/package_reference/big_modeling#accelerate.dispatch_model
  68. model = accelerate.dispatch_model(model, device_map=device_map, offload_buffers=True)
  69. # No offload
  70. elif not shared.args.cpu:
  71. model = model.to(torch.device('cuda:0'))
  72. return model