-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
67 lines (54 loc) · 1.83 KB
/
utils.py
File metadata and controls
67 lines (54 loc) · 1.83 KB
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
import os
import shutil
from transformers import AutoModelForCausalLM
from peft import PeftConfig, PeftModel
MODEL_NAME = 'm-a-p/CT-LLM-Base'
GRPO_MODEL_NAME = 'Qwen/Qwen2.5-1.5B-Instruct'
# GRPO_MODEL_NAME = 'Qwen/Qwen2.5-Coder-1.5B-Instruct'
def color_text(text, color):
COLOR_CODES = {
'black': '\033[90m',
'red': '\033[91m',
'green': '\033[92m',
'yellow': '\033[93m',
'blue': '\033[94m',
'magenta': '\033[95m',
'cyan': '\033[96m',
'white': '\033[97m',
'reset': '\033[0m'
}
return f"{COLOR_CODES.get(color, '')}{text}{COLOR_CODES['reset']}"
def center(text, fillchar="="):
width = shutil.get_terminal_size().columns
wrapped = text.center(width, fillchar)
return wrapped
def count_params(model):
count_all = [i.numel() for i in model.parameters()]
count_all = sum(count_all) / 1_000_000_000
count_require = [i.numel() for i in model.parameters() if i.requires_grad]
count_require = sum(count_require) / 1_000_000_000
ratio = count_require / count_all
return {
'count_require': count_require,
'count_all': count_all,
'ratio': ratio
}
def load_model(path, torch_dtype=None):
if os.path.exists(os.path.join(path, 'adapter_config.json')):
# LoRA adapter-only
peft_config = PeftConfig.from_pretrained(path)
base_model = AutoModelForCausalLM.from_pretrained(
peft_config.base_model_name_or_path,
trust_remote_code=True,
torch_dtype=torch_dtype,
device_map="auto"
)
model = PeftModel.from_pretrained(base_model, path)
else:
model = AutoModelForCausalLM.from_pretrained(
path,
trust_remote_code=True,
torch_dtype=torch_dtype,
device_map="auto"
)
return model