-
Notifications
You must be signed in to change notification settings - Fork 48
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
feat: Add support for jinja based template rendering of the dataset #438
Changes from all commits
902af4f
d65f759
1a4ef2e
0e9ad3f
8d3e77f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
dataprocessor: | ||
type: default | ||
datasets: | ||
- name: apply_custom_data_jinja_template | ||
data_paths: | ||
- "FILE_PATH" | ||
data_handlers: | ||
- name: apply_custom_data_formatting_jinja_template | ||
arguments: | ||
remove_columns: all | ||
batched: false | ||
fn_kwargs: | ||
dataset_text_field: "dataset_text_field" | ||
template: "dataset_template" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,12 @@ | |
import re | ||
|
||
# Third Party | ||
from jinja2 import Environment, StrictUndefined | ||
from transformers import AutoTokenizer | ||
|
||
# Local | ||
from tuning.utils.config_utils import process_jinja_placeholders | ||
|
||
|
||
### Utils for custom masking / manipulating input / output strs, etc | ||
def combine_sequence(input_element: str, output_element: str, eos_token: str = ""): | ||
|
@@ -108,6 +112,8 @@ def apply_custom_data_formatting_template( | |
Expects to be run as a HF Map API function. | ||
Args: | ||
element: the HF Dataset element loaded from a JSON or DatasetDict object. | ||
tokenizer: Tokenizer to be used for the EOS token, which will be appended | ||
when formatting the data into a single sequence. Defaults to empty. | ||
template: Template to format data with. Features of Dataset | ||
should be referred to by {{key}} | ||
formatted_dataset_field: Dataset_text_field | ||
|
@@ -137,6 +143,39 @@ def replace_text(match_obj): | |
} | ||
|
||
|
||
def apply_custom_data_formatting_jinja_template( | ||
element: Dict[str, str], | ||
tokenizer: AutoTokenizer, | ||
dataset_text_field: str, | ||
template: str, | ||
**kwargs, | ||
): | ||
"""Function to format datasets with jinja templates. | ||
Expects to be run as a HF Map API function. | ||
Args: | ||
element: the HF Dataset element loaded from a JSON or DatasetDict object. | ||
tokenizer: Tokenizer to be used for the EOS token, which will be appended | ||
when formatting the data into a single sequence. Defaults to empty. | ||
dataset_text_field: formatted_dataset_field. | ||
template: Template to format data with. Features of Dataset | ||
should be referred to by {{key}}. | ||
Returns: | ||
Formatted HF Dataset | ||
""" | ||
|
||
template += tokenizer.eos_token | ||
template = process_jinja_placeholders(template) | ||
env = Environment(undefined=StrictUndefined) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion @kmehant. @ashokponkumar @dushyantbehl @willmj If it sounds good to go ahead with this, I created a draft PR to include usage of |
||
jinja_template = env.from_string(template) | ||
|
||
try: | ||
rendered_text = jinja_template.render(element=element, **element) | ||
except Exception as e: | ||
raise KeyError(f"Dataset does not contain field in template. {e}") from e | ||
|
||
return {dataset_text_field: rendered_text} | ||
|
||
|
||
def apply_tokenizer_chat_template( | ||
element: Dict[str, str], | ||
tokenizer: AutoTokenizer, | ||
|
@@ -157,5 +196,6 @@ def apply_tokenizer_chat_template( | |
"tokenize_and_apply_input_masking": tokenize_and_apply_input_masking, | ||
"apply_dataset_formatting": apply_dataset_formatting, | ||
"apply_custom_data_formatting_template": apply_custom_data_formatting_template, | ||
"apply_custom_data_formatting_jinja_template": apply_custom_data_formatting_jinja_template, | ||
"apply_tokenizer_chat_template": apply_tokenizer_chat_template, | ||
} |
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.
Please add
tokenizer
to the args here.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.
Also I know this is not on you but can you please fix the doc string for line 104 as well.