models.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import json
  2. import os
  3. import re
  4. import time
  5. import zipfile
  6. from pathlib import Path
  7. import numpy as np
  8. import torch
  9. import transformers
  10. from accelerate import infer_auto_device_map, init_empty_weights
  11. from transformers import (AutoConfig, AutoModelForCausalLM, AutoTokenizer,
  12. BitsAndBytesConfig)
  13. import modules.shared as shared
  14. transformers.logging.set_verbosity_error()
  15. local_rank = None
  16. if shared.args.flexgen:
  17. from flexgen.flex_opt import CompressionConfig, ExecutionEnv, OptLM, Policy
  18. if shared.args.deepspeed:
  19. import deepspeed
  20. from transformers.deepspeed import (HfDeepSpeedConfig,
  21. is_deepspeed_zero3_enabled)
  22. from modules.deepspeed_parameters import generate_ds_config
  23. # Distributed setup
  24. local_rank = shared.args.local_rank if shared.args.local_rank is not None else int(os.getenv("LOCAL_RANK", "0"))
  25. world_size = int(os.getenv("WORLD_SIZE", "1"))
  26. torch.cuda.set_device(local_rank)
  27. deepspeed.init_distributed()
  28. ds_config = generate_ds_config(shared.args.bf16, 1 * world_size, shared.args.nvme_offload_dir)
  29. dschf = HfDeepSpeedConfig(ds_config) # Keep this object alive for the Transformers integration
  30. def load_model(model_name):
  31. print(f"Loading {model_name}...")
  32. t0 = time.time()
  33. shared.is_RWKV = 'rwkv-' in model_name.lower()
  34. shared.is_llamacpp = model_name.lower().startswith(('llamacpp', 'alpaca-cpp'))
  35. # Default settings
  36. if not any([shared.args.cpu, shared.args.load_in_8bit, shared.args.wbits, shared.args.auto_devices, shared.args.disk, shared.args.gpu_memory is not None, shared.args.cpu_memory is not None, shared.args.deepspeed, shared.args.flexgen, shared.is_RWKV, shared.is_llamacpp]):
  37. if any(size in shared.model_name.lower() for size in ('13b', '20b', '30b')):
  38. model = AutoModelForCausalLM.from_pretrained(Path(f"{shared.args.model_dir}/{shared.model_name}"), device_map='auto', load_in_8bit=True)
  39. else:
  40. model = AutoModelForCausalLM.from_pretrained(Path(f"{shared.args.model_dir}/{shared.model_name}"), low_cpu_mem_usage=True, torch_dtype=torch.bfloat16 if shared.args.bf16 else torch.float16)
  41. if torch.has_mps:
  42. device = torch.device('mps')
  43. model = model.to(device)
  44. else:
  45. model = model.cuda()
  46. # FlexGen
  47. elif shared.args.flexgen:
  48. # Initialize environment
  49. env = ExecutionEnv.create(shared.args.disk_cache_dir)
  50. # Offloading policy
  51. policy = Policy(1, 1,
  52. shared.args.percent[0], shared.args.percent[1],
  53. shared.args.percent[2], shared.args.percent[3],
  54. shared.args.percent[4], shared.args.percent[5],
  55. overlap=True, sep_layer=True, pin_weight=shared.args.pin_weight,
  56. cpu_cache_compute=False, attn_sparsity=1.0,
  57. compress_weight=shared.args.compress_weight,
  58. comp_weight_config=CompressionConfig(
  59. num_bits=4, group_size=64,
  60. group_dim=0, symmetric=False),
  61. compress_cache=False,
  62. comp_cache_config=CompressionConfig(
  63. num_bits=4, group_size=64,
  64. group_dim=2, symmetric=False))
  65. model = OptLM(f"facebook/{shared.model_name}", env, shared.args.model_dir, policy)
  66. # DeepSpeed ZeRO-3
  67. elif shared.args.deepspeed:
  68. model = AutoModelForCausalLM.from_pretrained(Path(f"{shared.args.model_dir}/{shared.model_name}"), torch_dtype=torch.bfloat16 if shared.args.bf16 else torch.float16)
  69. model = deepspeed.initialize(model=model, config_params=ds_config, model_parameters=None, optimizer=None, lr_scheduler=None)[0]
  70. model.module.eval() # Inference
  71. print(f"DeepSpeed ZeRO-3 is enabled: {is_deepspeed_zero3_enabled()}")
  72. # RMKV model (not on HuggingFace)
  73. elif shared.is_RWKV:
  74. from modules.RWKV import RWKVModel, RWKVTokenizer
  75. model = RWKVModel.from_pretrained(Path(f'{shared.args.model_dir}/{model_name}'), dtype="fp32" if shared.args.cpu else "bf16" if shared.args.bf16 else "fp16", device="cpu" if shared.args.cpu else "cuda")
  76. tokenizer = RWKVTokenizer.from_pretrained(Path(shared.args.model_dir))
  77. return model, tokenizer
  78. # Quantized model
  79. elif shared.args.wbits > 0:
  80. from modules.GPTQ_loader import load_quantized
  81. model = load_quantized(model_name)
  82. # llamacpp model
  83. elif shared.is_llamacpp:
  84. from modules.llamacpp_model import LlamaCppModel
  85. if model_name.lower().startswith('alpaca-cpp'):
  86. model_file = f'models/{model_name}/ggml-alpaca-7b-q4.bin'
  87. else:
  88. model_file = f'models/{model_name}/ggml-model-q4_0.bin'
  89. model, tokenizer = LlamaCppModel.from_pretrained(Path(model_file))
  90. return model, tokenizer
  91. # Custom
  92. else:
  93. params = {"low_cpu_mem_usage": True}
  94. if not any((shared.args.cpu, torch.cuda.is_available(), torch.has_mps)):
  95. print("Warning: torch.cuda.is_available() returned False.\nThis means that no GPU has been detected.\nFalling back to CPU mode.\n")
  96. shared.args.cpu = True
  97. if shared.args.cpu:
  98. params["torch_dtype"] = torch.float32
  99. else:
  100. params["device_map"] = 'auto'
  101. if shared.args.load_in_8bit and any((shared.args.auto_devices, shared.args.gpu_memory)):
  102. params['quantization_config'] = BitsAndBytesConfig(load_in_8bit=True, llm_int8_enable_fp32_cpu_offload=True)
  103. elif shared.args.load_in_8bit:
  104. params['quantization_config'] = BitsAndBytesConfig(load_in_8bit=True)
  105. elif shared.args.bf16:
  106. params["torch_dtype"] = torch.bfloat16
  107. else:
  108. params["torch_dtype"] = torch.float16
  109. if shared.args.gpu_memory:
  110. memory_map = list(map(lambda x : x.strip(), shared.args.gpu_memory))
  111. max_cpu_memory = shared.args.cpu_memory.strip() if shared.args.cpu_memory is not None else '99GiB'
  112. max_memory = {}
  113. for i in range(len(memory_map)):
  114. max_memory[i] = f'{memory_map[i]}GiB' if not re.match('.*ib$', memory_map[i].lower()) else memory_map[i]
  115. max_memory['cpu'] = max_cpu_memory
  116. params['max_memory'] = max_memory
  117. elif shared.args.auto_devices:
  118. total_mem = (torch.cuda.get_device_properties(0).total_memory / (1024*1024))
  119. suggestion = round((total_mem-1000) / 1000) * 1000
  120. if total_mem - suggestion < 800:
  121. suggestion -= 1000
  122. suggestion = int(round(suggestion/1000))
  123. print(f"\033[1;32;1mAuto-assiging --gpu-memory {suggestion} for your GPU to try to prevent out-of-memory errors.\nYou can manually set other values.\033[0;37;0m")
  124. max_memory = {0: f'{suggestion}GiB', 'cpu': f'{shared.args.cpu_memory or 99}GiB'}
  125. params['max_memory'] = max_memory
  126. if shared.args.disk:
  127. params["offload_folder"] = shared.args.disk_cache_dir
  128. checkpoint = Path(f'{shared.args.model_dir}/{shared.model_name}')
  129. if shared.args.load_in_8bit and params.get('max_memory', None) is not None and params['device_map'] == 'auto':
  130. config = AutoConfig.from_pretrained(checkpoint)
  131. with init_empty_weights():
  132. model = AutoModelForCausalLM.from_config(config)
  133. model.tie_weights()
  134. params['device_map'] = infer_auto_device_map(
  135. model,
  136. dtype=torch.int8,
  137. max_memory=params['max_memory'],
  138. no_split_module_classes = model._no_split_modules
  139. )
  140. model = AutoModelForCausalLM.from_pretrained(checkpoint, **params)
  141. # Loading the tokenizer
  142. if any((k in shared.model_name.lower() for k in ['gpt4chan', 'gpt-4chan'])) and Path(f"{shared.args.model_dir}/gpt-j-6B/").exists():
  143. tokenizer = AutoTokenizer.from_pretrained(Path(f"{shared.args.model_dir}/gpt-j-6B/"))
  144. else:
  145. tokenizer = AutoTokenizer.from_pretrained(Path(f"{shared.args.model_dir}/{shared.model_name}/"))
  146. tokenizer.truncation_side = 'left'
  147. print(f"Loaded the model in {(time.time()-t0):.2f} seconds.")
  148. return model, tokenizer
  149. def load_soft_prompt(name):
  150. if name == 'None':
  151. shared.soft_prompt = False
  152. shared.soft_prompt_tensor = None
  153. else:
  154. with zipfile.ZipFile(Path(f'softprompts/{name}.zip')) as zf:
  155. zf.extract('tensor.npy')
  156. zf.extract('meta.json')
  157. j = json.loads(open('meta.json', 'r').read())
  158. print(f"\nLoading the softprompt \"{name}\".")
  159. for field in j:
  160. if field != 'name':
  161. if type(j[field]) is list:
  162. print(f"{field}: {', '.join(j[field])}")
  163. else:
  164. print(f"{field}: {j[field]}")
  165. print()
  166. tensor = np.load('tensor.npy')
  167. Path('tensor.npy').unlink()
  168. Path('meta.json').unlink()
  169. tensor = torch.Tensor(tensor).to(device=shared.model.device, dtype=shared.model.dtype)
  170. tensor = torch.reshape(tensor, (1, tensor.shape[0], tensor.shape[1]))
  171. shared.soft_prompt = True
  172. shared.soft_prompt_tensor = tensor
  173. return name