GPTQ_loader.py 3.9 KB

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