Repository navigation
feat: add default option using transformers #67
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a116450
add transformers generation as default
EYH0602 c62956d
remove None option for router
EYH0602 6616d5c
remove vllm option for ease of dependency
EYH0602 6aae732
Update src/tfbench/lm/_hf.py
EYH0602 fdc28a3
Update src/tfbench/lm/_hf.py
EYH0602 ac87084
remove unnecessary imports
EYH0602 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 |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| from dotenv import dotenv_values | ||
| from dotenv import load_dotenv | ||
|
|
||
| ENV = dotenv_values(".env") | ||
|
|
||
| assert ENV, "No .env file found! Please create one with the required variables." | ||
| load_dotenv(override=True) # override existing env vars with those in .env |
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,56 @@ | ||
| from transformers import AutoModelForCausalLM, AutoTokenizer | ||
|
|
||
| from ._types import LM, LMAnswer, NoneResponseError | ||
|
|
||
|
|
||
| def extract_thinking_content(output: str) -> tuple[str, str | None]: | ||
| """Extract the thinking content and the final answer from the model output. | ||
| based on <think> and </think> tags. | ||
|
|
||
| Args: | ||
| output (str): The model output. | ||
| Returns: | ||
| tuple[str, str | None]: The thinking content and the final answer. | ||
| """ | ||
| if "<think>" in output and "</think>" in output: | ||
| thinking_content = output.split("<think>")[1].split("</think>")[0].strip() | ||
| content = output.split("</think>")[-1].strip() | ||
| return content, thinking_content | ||
|
|
||
| return output, None | ||
|
|
||
|
|
||
| class HFChat(LM): | ||
|
|
||
| def __init__(self, model_name: str, pure: bool = False): | ||
| super().__init__(model_name=model_name, pure=pure) | ||
|
|
||
| # load the tokenizer and the model | ||
| self.tokenizer = AutoTokenizer.from_pretrained(model_name) | ||
| self.model = AutoModelForCausalLM.from_pretrained( | ||
| model_name, torch_dtype="auto", device_map="auto" | ||
| ) | ||
|
|
||
| def _gen(self, prompt: str) -> LMAnswer: | ||
| messages = [ | ||
| {"role": "user", "content": prompt}, | ||
| {"role": "system", "content": self.instruction}, | ||
|
EYH0602 marked this conversation as resolved.
Outdated
|
||
| ] | ||
| text = self.tokenizer.apply_chat_template( | ||
| messages, | ||
| tokenize=False, | ||
| add_generation_prompt=True, | ||
| ) | ||
|
|
||
| model_inputs = self.tokenizer([text], return_tensors="pt").to(self.model.device) | ||
|
|
||
| # conduct text completion | ||
| generated_ids = self.model.generate(**model_inputs, max_new_tokens=32768) | ||
| output_ids = generated_ids[0][len(model_inputs.input_ids[0]) :].tolist() | ||
| output = self.tokenizer.decode(output_ids, skip_special_tokens=True).strip("\n") | ||
|
|
||
| if output is None: | ||
| raise NoneResponseError(self.model_name) | ||
|
|
||
|
EYH0602 marked this conversation as resolved.
Outdated
|
||
| content, thinking_content = extract_thinking_content(output) | ||
| return LMAnswer(answer=content, reasoning_steps=thinking_content) | ||
This file was deleted.
Oops, something went wrong.
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
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.
Uh oh!
There was an error while loading. Please reload this page.