-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
76 lines (62 loc) · 2.2 KB
/
utils.py
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
import logging
import os
from typing import Optional
import determined as det
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedModel
from transformers.trainer_utils import get_last_checkpoint
from chat_format import CHAT_ML_TEMPLATE
def get_model(model_name, inference=False, device_map="auto") -> PreTrainedModel:
if inference:
model = AutoModelForCausalLM.from_pretrained(
model_name, torch_dtype=torch.bfloat16, device_map=device_map
)
else:
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.bfloat16,
)
return model
def get_tokenizer(
model_name: str,
padding_side: str = "right",
truncation_side: str = "right",
model_max_length: Optional[int] = None,
add_eos_token: bool = False,
):
tokenizer = AutoTokenizer.from_pretrained(
model_name,
truncation_side=truncation_side,
padding_side=padding_side,
)
if tokenizer.pad_token_id is None:
tokenizer.pad_token_id = tokenizer.eos_token_id
if tokenizer.model_max_length > 100_000:
tokenizer.model_max_length = model_max_length if model_max_length else 2048
if tokenizer.chat_template is None:
tokenizer.chat_template = CHAT_ML_TEMPLATE
tokenizer.add_eos_token = add_eos_token
return tokenizer
def download_ckpt(ckpt_uuid: str, core_context: det.core.Context) -> str:
download_dir = os.path.join(os.environ.get("HF_CACHE", "."), ckpt_uuid)
def selector(path: str) -> bool:
if any(
[
path.endswith(ext)
for ext in [
"config.json",
"generation-config.json",
".safetensors",
"special_tokens_map.json",
"tokenizer_config.json",
"tokenizer.json",
"tokenizer.model",
"model.safetensors.index.json",
]
]
):
return True
return False
core_context.checkpoint.download(ckpt_uuid, download_dir, selector=selector)
model_dir = get_last_checkpoint(download_dir)
return model_dir