4
from typing import TYPE_CHECKING, Any, Dict
5
from datetime import datetime
7
from llmtuner.extras.packages import is_matplotlib_available
8
from llmtuner.extras.ploting import smooth
9
from llmtuner.webui.common import get_save_dir
12
from llmtuner.extras.callbacks import LogCallback
14
if is_matplotlib_available():
15
import matplotlib.figure
16
import matplotlib.pyplot as plt
19
def update_process_bar(callback: "LogCallback") -> Dict[str, Any]:
20
if not callback.max_steps:
21
return gr.update(visible=False)
23
percentage = round(100 * callback.cur_steps / callback.max_steps, 0) if callback.max_steps != 0 else 100.0
24
label = "Running {:d}/{:d}: {} < {}".format(
27
callback.elapsed_time,
28
callback.remaining_time
30
return gr.update(label=label, value=percentage, visible=True)
34
return datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
37
def can_quantize(finetuning_type: str) -> Dict[str, Any]:
38
if finetuning_type != "lora":
39
return gr.update(value="None", interactive=False)
41
return gr.update(interactive=True)
44
def gen_cmd(args: Dict[str, Any]) -> str:
45
args.pop("disable_tqdm", None)
46
args["plot_loss"] = args.get("do_train", None)
47
current_devices = os.environ.get("CUDA_VISIBLE_DEVICES", "0")
48
cmd_lines = ["CUDA_VISIBLE_DEVICES={} python src/train_bash.py ".format(current_devices)]
49
for k, v in args.items():
50
if v is not None and v != "":
51
cmd_lines.append(" --{} {} ".format(k, str(v)))
52
cmd_text = "\\\n".join(cmd_lines)
53
cmd_text = "```bash\n{}\n```".format(cmd_text)
57
def get_eval_results(path: os.PathLike) -> str:
58
with open(path, "r", encoding="utf-8") as f:
59
result = json.dumps(json.load(f), indent=4)
60
return "```json\n{}\n```\n".format(result)
63
def gen_plot(base_model: str, finetuning_type: str, output_dir: str) -> "matplotlib.figure.Figure":
66
log_file = get_save_dir(base_model, finetuning_type, output_dir, "trainer_log.jsonl")
67
if not os.path.isfile(log_file):
72
ax = fig.add_subplot(111)
73
steps, losses = [], []
74
with open(log_file, "r", encoding="utf-8") as f:
76
log_info = json.loads(line)
77
if log_info.get("loss", None):
78
steps.append(log_info["current_steps"])
79
losses.append(log_info["loss"])
84
ax.plot(steps, losses, alpha=0.4, label="original")
85
ax.plot(steps, smooth(losses), label="smoothed")