(
model_path, device, num_gpus, max_gpu_memory=None, load_8bit=False, lora_weights=None
)
| 70 | return gpu_memory |
| 71 | |
| 72 | def load_model( |
| 73 | model_path, device, num_gpus, max_gpu_memory=None, load_8bit=False, lora_weights=None |
| 74 | ): |
| 75 | if device == "cpu": |
| 76 | kwargs = {} |
| 77 | elif device == "cuda": |
| 78 | kwargs = {"torch_dtype": torch.float16} |
| 79 | if num_gpus == "auto": |
| 80 | kwargs["device_map"] = "auto" |
| 81 | else: |
| 82 | num_gpus = int(num_gpus) |
| 83 | if num_gpus != 1: |
| 84 | kwargs["device_map"] = "auto" |
| 85 | if max_gpu_memory is None: |
| 86 | kwargs[ |
| 87 | "device_map" |
| 88 | ] = "sequential" # This is important for not the same VRAM sizes |
| 89 | available_gpu_memory = get_gpu_memory(num_gpus) |
| 90 | kwargs["max_memory"] = { |
| 91 | i: str(int(available_gpu_memory[i] * 0.85)) + "GiB" |
| 92 | for i in range(num_gpus) |
| 93 | } |
| 94 | else: |
| 95 | kwargs["max_memory"] = {i: max_gpu_memory for i in range(num_gpus)} |
| 96 | else: |
| 97 | raise ValueError(f"Invalid device: {device}") |
| 98 | |
| 99 | tokenizer = LlamaTokenizer.from_pretrained( |
| 100 | model_path, use_fast=False) |
| 101 | |
| 102 | if lora_weights is None: |
| 103 | model = HuskyForConditionalGeneration.from_pretrained( |
| 104 | model_path, low_cpu_mem_usage=True, **kwargs |
| 105 | ) |
| 106 | else: |
| 107 | kwargs["device_map"] = "auto" |
| 108 | model = HuskyForConditionalGeneration.from_pretrained( |
| 109 | model_path, low_cpu_mem_usage=True, **kwargs |
| 110 | ) |
| 111 | model.language_model = PeftModel.from_pretrained( |
| 112 | model.language_model, |
| 113 | lora_weights, |
| 114 | **kwargs |
| 115 | ) |
| 116 | |
| 117 | if load_8bit: |
| 118 | compress_module(model, device) |
| 119 | |
| 120 | if (device == "cuda" and num_gpus == 1) or device == "mps": |
| 121 | model.to(device) |
| 122 | |
| 123 | model = model.eval() |
| 124 | return model, tokenizer |
| 125 | |
| 126 | def load_image(image_file, input_size=224): |
| 127 | if image_file.startswith('http') or image_file.startswith('https'): |
no test coverage detected