-
Notifications
You must be signed in to change notification settings - Fork 607
Factor out eager val from eval_llama_lib #3756
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
610643f
Factor out eager val from eval_llama_lib
cccclai 01ecc13
Update on "Factor out eager val from eval_llama_lib"
cccclai a2c7d6f
Update on "Factor out eager val from eval_llama_lib"
cccclai c3b8eeb
Update on "Factor out eager val from eval_llama_lib"
cccclai 3483891
Update on "Factor out eager val from eval_llama_lib"
cccclai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from .eager_eval import EagerEvalWrapper, evaluate_model | ||
|
||
__all__ = [ | ||
"evaluate_model", | ||
"EagerEvalWrapper", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
|
||
from typing import Optional, Union | ||
|
||
import lm_eval | ||
import torch | ||
from executorch.examples.models.llama2.tokenizer.tiktoken import Tokenizer as Tiktoken | ||
from executorch.examples.models.llama2.tokenizer.tokenizer import ( | ||
Tokenizer as SentencePieceTokenizer, | ||
) | ||
|
||
from lm_eval.api.model import LM | ||
from lm_eval.evaluator import evaluate | ||
from lm_eval.models.huggingface import HFLM as eval_wrapper | ||
from lm_eval.tasks import get_task_dict | ||
|
||
from torch import nn | ||
|
||
|
||
class EagerEvalWrapper(eval_wrapper): | ||
""" | ||
A wrapper class based on GPTFast, providing integration with the lm-evaluation-harness library. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
model: nn.Module, | ||
tokenizer: Union[SentencePieceTokenizer, Tiktoken], | ||
max_seq_length: Optional[int] = None, | ||
use_kv_cache: bool = False, | ||
): | ||
device = "cuda" if torch.cuda.is_available() else "cpu" | ||
super().__init__(device=device) | ||
self._model = model | ||
self._tokenizer = tokenizer | ||
self._device = torch.device(device) | ||
self._max_seq_length = 2048 if max_seq_length is None else max_seq_length | ||
self._use_kv_cache = use_kv_cache | ||
|
||
@property | ||
def eot_token_id(self): | ||
return self._tokenizer.eos_id | ||
|
||
@property | ||
def max_length(self): | ||
return self._max_seq_length | ||
|
||
@property | ||
def max_gen_toks(self): | ||
return 50 | ||
|
||
@property | ||
def batch_size(self): | ||
return 1 | ||
|
||
@property | ||
def device(self): | ||
return self._device | ||
|
||
def tok_encode(self, string: str, **kwargs): | ||
tokens = self._tokenizer.encode(string, bos=True, eos=False) | ||
encoded = torch.tensor(tokens, dtype=torch.int, device=self.device) | ||
# encoded is a pytorch tensor, but some internal logic in the | ||
# eval harness expects it to be a list instead | ||
# TODO: verify this for multi-batch as well | ||
encoded = encoded.tolist() | ||
return encoded | ||
|
||
def tok_decode(self, tokens): | ||
decoded = self._tokenizer.decode(tokens) | ||
return decoded | ||
|
||
def _model_call(self, inps): | ||
if self._use_kv_cache: | ||
pos_tensor = torch.arange( | ||
self._max_seq_length, dtype=torch.int64, device=self.device | ||
) | ||
|
||
# Batch process the whole sequence. | ||
logits = self._model(inps[:, : self._max_seq_length], pos_tensor) | ||
return logits | ||
else: | ||
return self._model(inps) | ||
|
||
def _model_generate(self, context, max_length, eos_token_id): | ||
raise Exception("unimplemented") | ||
|
||
|
||
@torch.no_grad() | ||
def evaluate_model( | ||
eval_wrapper: LM, | ||
tasks: Optional[list] = None, | ||
limit: Optional[int] = None, | ||
) -> dict: | ||
""" | ||
Evaluates a language model on a specified task using the lm-evaluation-harness library. | ||
|
||
Args: | ||
eval_wrapper (LM): A LM wrapper class compatible with lm-evaluation-harness evaluation | ||
task (str): The name of the evaluation task to perform. | ||
limit (Optional[int]): The maximum number of samples to evaluate (None for all available). | ||
|
||
Returns: | ||
eval_results (dict): A dictionary of evaluation results for the specified task(s). | ||
""" | ||
|
||
if tasks is None: | ||
tasks = ["wikitext"] | ||
|
||
if "hendrycks_test" in tasks: | ||
tasks.remove("hendrycks_test") | ||
tasks += list(lm_eval.tasks.hendrycks_test.create_all_tasks().keys()) | ||
task_dict = get_task_dict(tasks) | ||
|
||
eval_results = evaluate( | ||
eval_wrapper, | ||
task_dict, | ||
limit=limit, | ||
) | ||
return eval_results |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's put this as a task to fix separately. I've seem this issue in other parts of the repo too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah makes sense. Let me update