Commit 7ea81099 authored by chenych's avatar chenych
Browse files

update llama4

parent 84987715
This diff is collapsed.
This diff is collapsed.
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
import json import json
import os import os
from typing import TYPE_CHECKING, Any, Dict, List, Optional from typing import TYPE_CHECKING, Any, Optional
import numpy as np import numpy as np
import torch import torch
...@@ -59,7 +59,7 @@ if TYPE_CHECKING: ...@@ -59,7 +59,7 @@ if TYPE_CHECKING:
class Evaluator: class Evaluator:
def __init__(self, args: Optional[Dict[str, Any]] = None) -> None: def __init__(self, args: Optional[dict[str, Any]] = None) -> None:
self.model_args, self.data_args, self.eval_args, finetuning_args = get_eval_args(args) self.model_args, self.data_args, self.eval_args, finetuning_args = get_eval_args(args)
self.tokenizer = load_tokenizer(self.model_args)["tokenizer"] self.tokenizer = load_tokenizer(self.model_args)["tokenizer"]
self.tokenizer.padding_side = "right" # avoid overflow issue in batched inference for llama2 self.tokenizer.padding_side = "right" # avoid overflow issue in batched inference for llama2
...@@ -69,7 +69,7 @@ class Evaluator: ...@@ -69,7 +69,7 @@ class Evaluator:
self.choice_inputs = [self.tokenizer.encode(ch, add_special_tokens=False)[-1] for ch in CHOICES] self.choice_inputs = [self.tokenizer.encode(ch, add_special_tokens=False)[-1] for ch in CHOICES]
@torch.inference_mode() @torch.inference_mode()
def batch_inference(self, batch_input: Dict[str, "torch.Tensor"]) -> List[str]: def batch_inference(self, batch_input: dict[str, "torch.Tensor"]) -> list[str]:
logits = self.model(**batch_input).logits logits = self.model(**batch_input).logits
lengths = torch.sum(batch_input["attention_mask"], dim=-1) lengths = torch.sum(batch_input["attention_mask"], dim=-1)
word_probs = torch.stack([logits[i, lengths[i] - 1] for i in range(len(lengths))], dim=0) word_probs = torch.stack([logits[i, lengths[i] - 1] for i in range(len(lengths))], dim=0)
...@@ -88,7 +88,7 @@ class Evaluator: ...@@ -88,7 +88,7 @@ class Evaluator:
) )
with open(mapping, encoding="utf-8") as f: with open(mapping, encoding="utf-8") as f:
categorys: Dict[str, Dict[str, str]] = json.load(f) categorys: dict[str, dict[str, str]] = json.load(f)
category_corrects = {subj: np.array([], dtype="bool") for subj in SUBJECTS} category_corrects = {subj: np.array([], dtype="bool") for subj in SUBJECTS}
pbar = tqdm(categorys.keys(), desc="Processing subjects", position=0) pbar = tqdm(categorys.keys(), desc="Processing subjects", position=0)
...@@ -136,7 +136,7 @@ class Evaluator: ...@@ -136,7 +136,7 @@ class Evaluator:
pbar.close() pbar.close()
self._save_results(category_corrects, results) self._save_results(category_corrects, results)
def _save_results(self, category_corrects: Dict[str, "NDArray"], results: Dict[str, Dict[int, str]]) -> None: def _save_results(self, category_corrects: dict[str, "NDArray"], results: dict[str, dict[int, str]]) -> None:
score_info = "\n".join( score_info = "\n".join(
[ [
f"{category_name:>15}: {100 * np.mean(category_correct):.2f}" f"{category_name:>15}: {100 * np.mean(category_correct):.2f}"
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
# limitations under the License. # limitations under the License.
from dataclasses import dataclass from dataclasses import dataclass
from typing import Dict, List, Sequence, Tuple
from ..data import Role from ..data import Role
from ..extras.constants import CHOICES from ..extras.constants import CHOICES
...@@ -25,20 +24,19 @@ class EvalTemplate: ...@@ -25,20 +24,19 @@ class EvalTemplate:
choice: str choice: str
answer: str answer: str
def _parse_example(self, example: Dict[str, str]) -> Tuple[str, str]: def _parse_example(self, example: dict[str, str]) -> tuple[str, str]:
r""" r"""Parse eval example.
input: a dict with keys {"question", "A", "B", "C", "D", "answer"} input: a dict with keys {"question", "A", "B", "C", "D", "answer"}
output: a tuple of (prompt, response) output: a tuple of (prompt, response).
""" """
candidates = [self.choice.format(choice=ch, content=example[ch]) for ch in CHOICES if ch in example] candidates = [self.choice.format(choice=ch, content=example[ch]) for ch in CHOICES if ch in example]
return "".join([example["question"]] + candidates + [self.answer]), example["answer"] return "".join([example["question"]] + candidates + [self.answer]), example["answer"]
def format_example( def format_example(
self, target_data: Dict[str, str], support_set: Sequence[Dict[str, str]], subject_name: str self, target_data: dict[str, str], support_set: list[dict[str, str]], subject_name: str
) -> List[Dict[str, str]]: ) -> list[dict[str, str]]:
r""" r"""Convert dataset examples to messages."""
Converts dataset examples to messages.
"""
messages = [] messages = []
for k in range(len(support_set)): for k in range(len(support_set)):
prompt, response = self._parse_example(support_set[k]) prompt, response = self._parse_example(support_set[k])
...@@ -52,7 +50,7 @@ class EvalTemplate: ...@@ -52,7 +50,7 @@ class EvalTemplate:
return messages return messages
eval_templates: Dict[str, "EvalTemplate"] = {} eval_templates: dict[str, "EvalTemplate"] = {}
def _register_eval_template(name: str, system: str, choice: str, answer: str) -> None: def _register_eval_template(name: str, system: str, choice: str, answer: str) -> None:
......
This diff is collapsed.
# Copyright 2024 HuggingFace Inc. and the LlamaFactory team. # Copyright 2025 HuggingFace Inc. and the LlamaFactory team.
# #
# This code is inspired by the HuggingFace's transformers library. # This code is inspired by the HuggingFace's transformers library.
# https://github.com/huggingface/transformers/blob/v4.40.0/src/transformers/commands/env.py # https://github.com/huggingface/transformers/blob/v4.40.0/src/transformers/commands/env.py
...@@ -26,7 +26,7 @@ import trl ...@@ -26,7 +26,7 @@ import trl
from transformers.utils import is_torch_cuda_available, is_torch_npu_available from transformers.utils import is_torch_cuda_available, is_torch_npu_available
VERSION = "0.9.2" VERSION = "0.9.3.dev0"
def print_env() -> None: def print_env() -> None:
......
This diff is collapsed.
This diff is collapsed.
# Copyright 2024 HuggingFace Inc. and the LlamaFactory team. # Copyright 2025 HuggingFace Inc. and the LlamaFactory team.
# #
# This code is inspired by the HuggingFace's transformers library. # This code is inspired by the HuggingFace's transformers library.
# https://github.com/huggingface/transformers/blob/v4.40.0/src/transformers/utils/import_utils.py # https://github.com/huggingface/transformers/blob/v4.40.0/src/transformers/utils/import_utils.py
...@@ -97,3 +97,7 @@ def is_uvicorn_available(): ...@@ -97,3 +97,7 @@ def is_uvicorn_available():
def is_vllm_available(): def is_vllm_available():
return _is_package_available("vllm") return _is_package_available("vllm")
def is_sglang_available():
return _is_package_available("sglang")
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
import json import json
import math import math
import os import os
from typing import Any, Dict, List from typing import Any
from transformers.trainer import TRAINER_STATE_NAME from transformers.trainer import TRAINER_STATE_NAME
...@@ -31,10 +31,8 @@ if is_matplotlib_available(): ...@@ -31,10 +31,8 @@ if is_matplotlib_available():
logger = logging.get_logger(__name__) logger = logging.get_logger(__name__)
def smooth(scalars: List[float]) -> List[float]: def smooth(scalars: list[float]) -> list[float]:
r""" r"""EMA implementation according to TensorBoard."""
EMA implementation according to TensorBoard.
"""
if len(scalars) == 0: if len(scalars) == 0:
return [] return []
...@@ -48,10 +46,8 @@ def smooth(scalars: List[float]) -> List[float]: ...@@ -48,10 +46,8 @@ def smooth(scalars: List[float]) -> List[float]:
return smoothed return smoothed
def gen_loss_plot(trainer_log: List[Dict[str, Any]]) -> "matplotlib.figure.Figure": def gen_loss_plot(trainer_log: list[dict[str, Any]]) -> "matplotlib.figure.Figure":
r""" r"""Plot loss curves in LlamaBoard."""
Plots loss curves in LlamaBoard.
"""
plt.close("all") plt.close("all")
plt.switch_backend("agg") plt.switch_backend("agg")
fig = plt.figure() fig = plt.figure()
...@@ -70,10 +66,8 @@ def gen_loss_plot(trainer_log: List[Dict[str, Any]]) -> "matplotlib.figure.Figur ...@@ -70,10 +66,8 @@ def gen_loss_plot(trainer_log: List[Dict[str, Any]]) -> "matplotlib.figure.Figur
return fig return fig
def plot_loss(save_dictionary: str, keys: List[str] = ["loss"]) -> None: def plot_loss(save_dictionary: str, keys: list[str] = ["loss"]) -> None:
r""" r"""Plot loss curves and saves the image."""
Plots loss curves and saves the image.
"""
plt.switch_backend("agg") plt.switch_backend("agg")
with open(os.path.join(save_dictionary, TRAINER_STATE_NAME), encoding="utf-8") as f: with open(os.path.join(save_dictionary, TRAINER_STATE_NAME), encoding="utf-8") as f:
data = json.load(f) data = json.load(f)
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment