Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fewshot and apply chat template lm_eval functionality #1180

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions llms/mlx_lm/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def __init__(
self._model, self._tokenizer = load(path_or_hf_repo)
self._max_tokens = max_tokens or self._tokenizer.model_max_length

# Needed by HF implementation methods (tokenizer_name, apply_chat_template, and, tok_encode)
self.tokenizer = self._tokenizer

def _score_fn(self, inputs, tokenize=True, step_size=32):
if tokenize:
inputs = self._tokenizer.encode(inputs)
Expand Down Expand Up @@ -217,6 +220,10 @@ def loglikelihood(self, requests) -> list[tuple[float, bool]]:
)
return [(r[0], r[1] == r[2]) for r in results]

tokenizer_name = lm_eval.models.huggingface.HFLM.tokenizer_name
apply_chat_template = lm_eval.models.huggingface.HFLM.apply_chat_template
tok_encode = lm_eval.models.huggingface.HFLM.tok_encode

def loglikelihood_rolling(self, requests) -> list[float]:
"""Compute full log-likelihood of a string, with no truncation, for perplexity computation
- We will use the full max context length of the model.
Expand Down Expand Up @@ -322,6 +329,18 @@ def main():
help="Maximum nunber of tokens to generate. Defaults to the model's max context length.",
)
parser.add_argument("--seed", type=int, default=123, help="Random seed.")
parser.add_argument(
"--fewshot-as-multiturn",
action="store_true",
help="Whether to provide the fewshot examples as a multiturn conversation or a single user turn.",
default=False,
)
parser.add_argument(
"--apply-chat-template",
action="store_true",
help="Specifies whether to apply a chat template to the prompt",
default=False,
)
args = parser.parse_args()

output_dir = Path(args.output_dir)
Expand All @@ -337,6 +356,8 @@ def main():
results = lm_eval.simple_evaluate(
model=lm,
tasks=args.tasks,
fewshot_as_multiturn=args.fewshot_as_multiturn,
apply_chat_template=args.apply_chat_template,
num_fewshot=args.num_shots,
random_seed=args.seed,
numpy_random_seed=args.seed,
Expand Down