Spaces:
Runtime error
Runtime error
File size: 2,612 Bytes
4df8249 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
from optimum.bettertransformer import BetterTransformer
def load_model(
base,
finetuned,
mode_cpu,
mode_mps,
mode_full_gpu,
mode_8bit,
mode_4bit,
force_download_ckpt,
local_files_only
):
tokenizer = AutoTokenizer.from_pretrained(
base, local_files_only=local_files_only
)
if mode_cpu:
print("cpu mode")
model = AutoModelForCausalLM.from_pretrained(
base,
device_map={"": "cpu"},
use_safetensors=False,
local_files_only=local_files_only
)
if finetuned is not None and \
finetuned != "" and \
finetuned != "N/A":
model = PeftModel.from_pretrained(
model,
finetuned,
device_map={"": "cpu"},
# force_download=force_download_ckpt,
)
else:
model = BetterTransformer.transform(model)
elif mode_mps:
print("mps mode")
model = AutoModelForCausalLM.from_pretrained(
base,
device_map={"": "mps"},
torch_dtype=torch.float16,
use_safetensors=False,
local_files_only=local_files_only
)
if finetuned is not None and \
finetuned != "" and \
finetuned != "N/A":
model = PeftModel.from_pretrained(
model,
finetuned,
torch_dtype=torch.float16,
device_map={"": "mps"},
# force_download=force_download_ckpt,
)
else:
model = BetterTransformer.transform(model)
else:
print("gpu mode")
print(f"8bit = {mode_8bit}, 4bit = {mode_4bit}")
model = AutoModelForCausalLM.from_pretrained(
base,
load_in_8bit=mode_8bit,
load_in_4bit=mode_4bit,
device_map="auto",
use_safetensors=False,
local_files_only=local_files_only
)
if not mode_8bit and not mode_4bit:
model.half()
if finetuned is not None and \
finetuned != "" and \
finetuned != "N/A":
model = PeftModel.from_pretrained(
model,
finetuned,
# force_download=force_download_ckpt,
)
else:
model = BetterTransformer.transform(model)
return model, tokenizer |