Skip to content

Commit

Permalink
Added Hellaswag HF's preprocessor logic as custom script. (#1631)
Browse files Browse the repository at this point in the history
* Added pre-processor component and it's associated functionalities and methods for AML Benchmark

* Added pre-processor component and it's associated functionalities and methods for AML Benchmark

* Added pre-processor component and it's associated functionalities and methods for AML Benchmark

* Added dataset pre-processor and inference post-processor (3P) components and it's associated functionalities and methods for AML Benchmark

* Added dataset pre-processor and inference post-processor (3P) components and it's associated functionalities and methods for AML Benchmark

* Added dataset pre-processor and inference post-processor (3P) components and it's associated functionalities and methods for AML Benchmark, 'safe to test'

* Changed the import from test_utils instead of utils.

* Added the modification to resolve the suggested the comments.

* Added the modification to resolve the suggested the comments.

* Updated the component version.

* Added the modification and tests cases to resolve the suggested the comments. Added two new inputs to post processor component.

* Added the modification and tests cases to resolve the suggested the comments. Added two new inputs to post processor component.

* Added revised components and required functionalties in post-processor component to get shared as package.

* Added revised components and required functionalties in post-processor component to get shared as package.

* Added revised components and required functionalties in post-processor component to get shared as package.

* Added revised components and required functionalties in post-processor component to get shared as package.

* Added revised components and required functionalties in post-processor component to get shared as package.

* branch containing changes to clone in  devbox

* branch containing changes to clone in  devbox

* Post processor component converged

* Removed the unused files.

* Removed the unused files.

* Fixed the overwritten changes by git pull.

* Changed the output path to comply with pipeline schema change.

* Added the suggestions provided in PR review comments.

* Added the suggestions provided in PR review comments.

* removed unused files.

* Fixed the custom script post processor method in the inference postprocessor component.

* 1. Fixed custom script method call in post processor. 2. Added truthful_qa dataset custorm preprocessor.

* 1. Fixed custom script method call in post processor. 2. Added truthful_qa dataset custorm preprocessor.

* Added the reviewed comments provided by reviewers.

* Added the reviewed comments provided by reviewers.

* Added Hellaswag HF's preprocessor logic as custom script.

* Added the suggestions provided by reviewer.

---------

Co-authored-by: [email protected] <[email protected]>
  • Loading branch information
Nivi09 and [email protected] authored Nov 2, 2023
1 parent 68c6220 commit c82da79
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ---------------------------------------------------------

"""Custom preprocessor script for dataset:Hellaswag, source:HF."""

from typing import Any, Dict, List, Union
import argparse
import json
import pandas as pd
import re


def _parse_args():
"""Parse the arguments."""
parser = argparse.ArgumentParser()
parser.add_argument('--input_path', type=str, required=True)
parser.add_argument('--output_path', type=str, required=True)
args = parser.parse_args()
return args


def _read_jsonl_file(file_path: str) -> List[Dict[str, Any]]:
"""Read `.jsonl` file and return a list of dictionaries."""
if not file_path.endswith(".jsonl"):
mssg = f"Input file '{file_path}' is not a .jsonl file."
raise ValueError(mssg)
data_dicts = []
with open(file_path, 'r', encoding="utf8") as file:
for i, line in enumerate(file):
data_dicts.append(json.loads(line))
return data_dicts


def _write_to_jsonl_file(data, file_path: str) -> None:
"""Write the processed output to jsonl file."""
if isinstance(data, pd.DataFrame):
data.to_json(file_path, lines=True, orient='records')
return
if isinstance(data, List):
with open(file_path, 'w') as writer:
for example in data:
writer.write(json.dumps(example) + "\n")
return


def _run(input_path: str, output_path: str) -> None:
"""Entry function to read, run and write the processed the data."""
data = _read_jsonl_file(input_path)
processed_data = run_processor(data)
_write_to_jsonl_file(processed_data, output_path)


def preprocess(text: str) -> str:
"""Clean the data."""
text = text.strip()
# NOTE: Brackets are artifacts of the WikiHow dataset portion of HellaSwag. We will clean these artifacts.
text = text.replace(" [title]", ". ")
# the re.sub("\\[.*?\\]", "", text) function will remove any text enclosed in square brackets from the
# input string text.
text = re.sub("\\[.*?\\]", "", text)
text = text.replace(" ", " ")
return text


def run_processor(data: List[Dict[str, Any]]) -> Union[pd.DataFrame, List[Dict[str, Any]]]:
"""Run the custom processor function."""
ret_data = []
for doc in data:
ctx = doc["ctx_a"] + " " + doc["ctx_b"].capitalize()
out_doc = {
"query": preprocess(doc["activity_label"] + ": " + ctx),
"choices": [preprocess(ending) for ending in doc["endings"]],
"gold": str(doc["label"]),
}
ret_data.append(out_doc)
return ret_data


if __name__ == '__main__':
args = _parse_args()
_run(args.input_path, args.output_path)
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
{"name":"quac_org","data": [{"paragraphs": [{"context": "In May 1983, she married Nikos Karvelas, a composer, with whom she collaborated in 1975 and in November she gave birth to her daughter Sofia. After their marriage, she started a close collaboration with Karvelas. Since 1975, all her releases have become gold or platinum and have included songs by Karvelas. In 1986, she participated at the Cypriot National Final for Eurovision Song Contest with the song Thelo Na Gino Star (\"I Want To Be A Star\"), taking second place. This song is still unreleased up to date. In 1984, Vissi left her record company EMI Greece and signed with CBS Records Greece, which later became Sony Music Greece, a collaboration that lasted until 2013. In March 1984, she released Na 'Hes Kardia (\"If You Had a Heart\"). The album was certified gold. The following year her seventh album Kati Simveni (\"Something Is Happening\") was released which included one of her most famous songs, titled \"Dodeka\" [\"Twelve (O'Clock)\"] and reached gold status selling 80.000 units. In 1986 I Epomeni Kinisi (\"The Next Move\") was released. The album included the hit Pragmata (\"Things\") and went platinum, becoming the best selling record of the year. In February 1988 she released her ninth album Tora (\"Now\") and in December the album Empnefsi! (\"Inspiration!\") which went gold. In 1988, she made her debut as a radio producer on ANT1 Radio. Her radio program was titled after one of her songs Ta Koritsia Einai Atakta (\"Girls Are Naughty\") and was aired every weekend. In the same year, she participated with the song Klaio (\"I'm Crying\") at the Greek National Final for Eurovision Song Contest, finishing third. In 1989, she released the highly successful studio album Fotia (Fire), being one of the first albums to feature western sounds. The lead single Pseftika (\"Fake\") became a big hit and the album reached platinum status, selling 180.000 copies and becoming the second best selling record of 1990. She performed at \"Diogenis Palace\" in that same year, Athens's biggest nightclub/music hall at the time. CANNOTANSWER","qas": [{"followup": "y","yesno": "x","question": "what happened in 1983?","answers": [{"text": "In May 1983, she married Nikos Karvelas,","answer_start": 0},{"text": "In May 1983, she married Nikos Karvelas, a composer,","answer_start": 0},{"text": "In May 1983, she married Nikos Karvelas,","answer_start": 0},{"text": "In May 1983, she married Nikos Karvelas, a composer,","answer_start": 0}],"id": "C_5ab583f64dbb47b995cf59328ea0af43_1_q#0","orig_answer": {"text": "In May 1983, she married Nikos Karvelas, a composer,","answer_start": 0}},{"followup": "m","yesno": "y","question": "did they have any children?","answers": [{"text": "she gave birth to her daughter Sofia.","answer_start": 104},{"text": "in 1975 and in November she gave birth to her daughter Sofia.","answer_start": 80},{"text": "her daughter Sofia.","answer_start": 122},{"text": "in November she gave birth to her daughter Sofia.","answer_start": 92}],"id": "C_5ab583f64dbb47b995cf59328ea0af43_1_q#1","orig_answer": {"text": "in November she gave birth to her daughter Sofia.","answer_start": 92}},{"followup": "n","yesno": "x","question": "did she have any other children?","answers": [{"text": "CANNOTANSWER","answer_start": 2024},{"text": "CANNOTANSWER","answer_start": 2024},{"text": "CANNOTANSWER","answer_start": 2024},{"text": "CANNOTANSWER","answer_start": 2024}],"id": "C_5ab583f64dbb47b995cf59328ea0af43_1_q#2","orig_answer": {"text": "CANNOTANSWER","answer_start": 2024}},{"followup": "m","yesno": "x","question": "what collaborations did she do with nikos?","answers": [{"text": "After their marriage, she started a close collaboration with Karvelas. Since 1975, all her releases have become gold or platinum and have included songs by Karvelas.","answer_start": 142},{"text": "a composer, with whom she collaborated in 1975","answer_start": 41},{"text": "Thelo Na Gino Star (\"","answer_start": 406},{"text": "Since 1975, all her releases have become gold or platinum and have included songs by Karvelas.","answer_start": 213}],"id": "C_5ab583f64dbb47b995cf59328ea0af43_1_q#3","orig_answer": {"text": "Since 1975, all her releases have become gold or platinum and have included songs by Karvelas.","answer_start": 213}},{"followup": "n","yesno": "x","question": "what influences does he have in her music?","answers": [{"text": "CANNOTANSWER","answer_start": 2024},{"text": "CANNOTANSWER","answer_start": 2024},{"text": "CANNOTANSWER","answer_start": 2024},{"text": "CANNOTANSWER","answer_start": 2024}],"id": "C_5ab583f64dbb47b995cf59328ea0af43_1_q#4","orig_answer": {"text": "CANNOTANSWER","answer_start": 2024}},{"followup": "y","yesno": "x","question": "what were some of the songs?","answers": [{"text": "Thelo Na Gino Star (\"I Want To Be A Star\"),","answer_start": 406},{"text": "Thelo Na Gino Star (\"","answer_start": 406},{"text": "one of her most famous songs, titled \"Dodeka\" [\"Twelve (O'Clock)\"]","answer_start": 879}],"id": "C_5ab583f64dbb47b995cf59328ea0af43_1_q#5","orig_answer": {"text": "one of her most famous songs, titled \"Dodeka\" [\"Twelve (O'Clock)\"]","answer_start": 879}},{"followup": "y","yesno": "x","question": "how famous was it?","answers": [{"text": "reached gold status selling 80.000 units.","answer_start": 950},{"text": "reached gold status selling 80.000 units.","answer_start": 950},{"text": "reached gold status selling 80.000 units.","answer_start": 950},{"text": "reached gold status selling 80.000 units.","answer_start": 950}],"id": "C_5ab583f64dbb47b995cf59328ea0af43_1_q#6","orig_answer": {"text": "reached gold status selling 80.000 units.","answer_start": 950}},{"followup": "y","yesno": "y","question": "did she have any other famous songs?","answers": [{"text": "In 1986 I Epomeni Kinisi (\"The Next Move\") was released.","answer_start": 992},{"text": "Epomeni Kinisi (\"The Next Move\") was released.","answer_start": 1002},{"text": "Pragmata (\"Things\")","answer_start": 1076},{"text": "The album included the hit Pragmata (\"Things\") and went platinum,","answer_start": 1049}],"id": "C_5ab583f64dbb47b995cf59328ea0af43_1_q#7","orig_answer": {"text": "The album included the hit Pragmata (\"Things\") and went platinum,","answer_start": 1049}}],"id": "C_5ab583f64dbb47b995cf59328ea0af43_1"}],"section_title": "1983-1989: Collaboration with Nikos Karvelas","background": "Anna Vissi (Greek: Anna Bisse (pronounced ['ana 'visi], locally ['an:a 'viS:i]); born 20 December 1957), also known as Anna Vishy, is a Greek Cypriot singer, songwriter, actress, television presenter, radio personality, and businesswoman.","title": "Anna Vissi"}]}
{"name":"mnli","promptID":63735,"pairID":"63735n","premise":"The new rights are nice enough","premise_binary_parse":"( ( The ( new rights ) ) ( are ( nice enough ) ) )","premise_parse":"(ROOT (S (NP (DT The) (JJ new) (NNS rights)) (VP (VBP are) (ADJP (JJ nice) (RB enough)))))","hypothesis":"Everyone really likes the newest benefits ","hypothesis_binary_parse":"( Everyone ( really ( likes ( the ( newest benefits ) ) ) ) )","hypothesis_parse":"(ROOT (S (NP (NN Everyone)) (VP (ADVP (RB really)) (VBZ likes) (NP (DT the) (JJS newest) (NNS benefits)))))","genre":"slate","label":1}
{"name":"human-eval-babel", "prompt": "from typing import List\n\n\ndef has_close_elements(numbers: List[float], threshold: float) -> bool:\n \"\"\" Check if in given list of numbers, are any two numbers closer to each other than\n given threshold.\n >>> has_close_elements([1.0, 2.0, 3.0], 0.5)\n False\n >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)\n True\n \"\"\"\n", "completion": "HumanEval/0", "metadata": {"task_id": "HumanEval/0"}, "prompt_length": 46, "samples": " for i in range(len(numbers) - 1):\n for j in range(i + 1, len(numbers)):\n if abs(numbers[i] - numbers[j]) < threshold:\n return True\n return False\n\n\ndef has_close_elements_2(numbers: List[float], threshold: float) -> bool:\n \"\"\" Check if in given list of numbers, are any two numbers closer to each other than\n given threshold.\n >>> has_close_elements([1.0, 2.0, 3.0], 0.5)\n False\n >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)\n True\n \"\"\"\n for i in range(len(numbers) - 1):\n for j in range(i + 1, len(numbers)):\n if abs(numbers[i] - numbers[j]) < threshold:\n return True\n return False\n\n\ndef has_close_elements_3(numbers: List[float], threshold: float) -> bool:\n \"\"\" Check if in given list of numbers, are any two numbers closer to each other than\n given threshold.\n >>> has_close_elements([1.0, 2.0, 3.0], 0.5)\n False\n >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)\n True\n \"\"\"\n for i in range(len(numbers) - 1):\n for j in range(i + 1, len(numbers)):\n if abs(numbers[i] - numbers[j]) < threshold:\n return True\n return False\n\n\ndef has_close_elements_4(numbers: List[float], threshold: float) -> bool:\n \"\"\" Check if in given list of numbers, are any two numbers closer to each other than\n given threshold.\n >>> has_close_elements([1.0, 2.0, 3.0],"}
{"name":"human-eval", "task_id":"HumanEval\/0","prompt":"from typing import List\n\n\ndef has_close_elements(numbers: List[float], threshold: float) -> bool:\n \"\"\" Check if in given list of numbers, are any two numbers closer to each other than\n given threshold.\n >>> has_close_elements([1.0, 2.0, 3.0], 0.5)\n False\n >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)\n True\n \"\"\"\n","canonical_solution":" for idx, elem in enumerate(numbers):\n for idx2, elem2 in enumerate(numbers):\n if idx != idx2:\n distance = abs(elem - elem2)\n if distance < threshold:\n return True\n\n return False\n","test":"\n\nMETADATA = {\n 'author': 'jt',\n 'dataset': 'test'\n}\n\n\ndef check(candidate):\n assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.3) == True\n assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.05) == False\n assert candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.95) == True\n assert candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.8) == False\n assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0], 0.1) == True\n assert candidate([1.1, 2.2, 3.1, 4.1, 5.1], 1.0) == True\n assert candidate([1.1, 2.2, 3.1, 4.1, 5.1], 0.5) == False\n\n","entry_point":"has_close_elements"}
{"name":"human-eval", "task_id":"HumanEval\/0","prompt":"from typing import List\n\n\ndef has_close_elements(numbers: List[float], threshold: float) -> bool:\n \"\"\" Check if in given list of numbers, are any two numbers closer to each other than\n given threshold.\n >>> has_close_elements([1.0, 2.0, 3.0], 0.5)\n False\n >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)\n True\n \"\"\"\n","canonical_solution":" for idx, elem in enumerate(numbers):\n for idx2, elem2 in enumerate(numbers):\n if idx != idx2:\n distance = abs(elem - elem2)\n if distance < threshold:\n return True\n\n return False\n","test":"\n\nMETADATA = {\n 'author': 'jt',\n 'dataset': 'test'\n}\n\n\ndef check(candidate):\n assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.3) == True\n assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.05) == False\n assert candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.95) == True\n assert candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.8) == False\n assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0], 0.1) == True\n assert candidate([1.1, 2.2, 3.1, 4.1, 5.1], 1.0) == True\n assert candidate([1.1, 2.2, 3.1, 4.1, 5.1], 0.5) == False\n\n","entry_point":"has_close_elements"}
{"name":"math_hf", "problem":"Captain Rusczyk tracked down a pirate who had stolen $2345_{6}$ dollars worth of goods from his ship. After winning an epic duel, the Captain demands that the pirate return $41324_{5}$ dollars. How much has the pirate gone in debt due to his two encounters with Rusczyk? Express your answer in base 10.","solution":"We wish to solve for $41324_{5}-2345_{6}$.$2345_{6} = 5\\cdot6^{0}+4\\cdot6^{1}+3\\cdot6^{2}+2\\cdot6^{3} = 5+24+108+432 = 569_{10}$$41324_{5} = 4\\cdot5^{0}+2\\cdot5^{1}+3\\cdot5^{2}+1\\cdot5^{3}+4\\cdot5^{4} = 4+10+75+125+2500 = 2714_{10}$So, the pirate is now $2714-569= \\boxed{2145}$ dollars in debt.","answer":"2145"}
{"name":"math_hf", "problem":"We have a triangle $\\triangle ABC$ where $AC = 17,$ $BC = 15,$ and $AB = 8.$ Let $M$ be the midpoint of $AB.$ What is the length of $CM$?","solution":"First, we notice that $8:15:17$ is a Pythagorean triple, so we have a right triangle. Let's draw a sketch: [asy]pair A, B, C, M;A = (0, 8);B = (0, 0);C = (15, 0);M = 0.5 * A + 0.5 * B;draw(A--B--C--cycle);draw(C--M);label(\"$A$\", A, N);label(\"$B$\", B, SW);label(\"$C$\", C, SE);label(\"$M$\", M, NE);label(\"$8$\", A--B, W);label(\"$15$\", B--C, S);label(\"$17$\", A--C, NE);draw(rightanglemark(A,B,C,20));[\/asy] Since we know that $BM = \\frac{1}{2} \\cdot AB = 4,$ we simply apply the Pythagorean theorem to right triangle $\\triangle MBC.$ \\begin{align*}CM^2 &= BM^2 + BC^2\\CM^2 &= 4^2 + 15^2 = 241\\CM &= \\boxed{\\sqrt{241}}.\\end{align*}","answer":"\\sqrt{241}"}
{"name":"hellaswag_hf","ind":24,"activity_label":"Roof shingle removal","ctx_a":"A man is sitting on a roof.","ctx_b":"he","ctx":"A man is sitting on a roof. he","endings":["is using wrap to wrap a pair of skis.","is ripping level tiles off.","is holding a rubik's cube.","starts pulling up roofing on a roof."],"source_id":"activitynet~v_-JhWjGDPHMY","split":"val","split_type":"indomain","label":"3"}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
{"name":"quac_org", "question": "did she have any other famous songs?", "answers": ["In 1986 I Epomeni Kinisi (\"The Next Move\") was released.", "Epomeni Kinisi (\"The Next Move\") was released.", "Pragmata (\"Things\")", "The album included the hit Pragmata (\"Things\") and went platinum,", "The album included the hit Pragmata (\"Things\") and went platinum,"], "context": "In May 1983, she married Nikos Karvelas, a composer, with whom she collaborated in 1975 and in November she gave birth to her daughter Sofia. After their marriage, she started a close collaboration with Karvelas. Since 1975, all her releases have become gold or platinum and have included songs by Karvelas. In 1986, she participated at the Cypriot National Final for Eurovision Song Contest with the song Thelo Na Gino Star (\"I Want To Be A Star\"), taking second place. This song is still unreleased up to date. In 1984, Vissi left her record company EMI Greece and signed with CBS Records Greece, which later became Sony Music Greece, a collaboration that lasted until 2013. In March 1984, she released Na 'Hes Kardia (\"If You Had a Heart\"). The album was certified gold. The following year her seventh album Kati Simveni (\"Something Is Happening\") was released which included one of her most famous songs, titled \"Dodeka\" [\"Twelve (O'Clock)\"] and reached gold status selling 80.000 units. In 1986 I Epomeni Kinisi (\"The Next Move\") was released. The album included the hit Pragmata (\"Things\") and went platinum, becoming the best selling record of the year. In February 1988 she released her ninth album Tora (\"Now\") and in December the album Empnefsi! (\"Inspiration!\") which went gold. In 1988, she made her debut as a radio producer on ANT1 Radio. Her radio program was titled after one of her songs Ta Koritsia Einai Atakta (\"Girls Are Naughty\") and was aired every weekend. In the same year, she participated with the song Klaio (\"I'm Crying\") at the Greek National Final for Eurovision Song Contest, finishing third. In 1989, she released the highly successful studio album Fotia (Fire), being one of the first albums to feature western sounds. The lead single Pseftika (\"Fake\") became a big hit and the album reached platinum status, selling 180.000 copies and becoming the second best selling record of 1990. She performed at \"Diogenis Palace\" in that same year, Athens's biggest nightclub/music hall at the time. CANNOTANSWER", "history": "what happened in 1983?\nIn May 1983, she married Nikos Karvelas, a composer,\ndid they have any children?\nin November she gave birth to her daughter Sofia.\nwhat collaborations did she do with nikos?\nSince 1975, all her releases have become gold or platinum and have included songs by Karvelas.\nwhat were some of the songs?\none of her most famous songs, titled \"Dodeka\" [\"Twelve (O'Clock)\"]\nhow famous was it?\nreached gold status selling 80.000 units."}
{"name":"mnli", "premise": "The new rights are nice enough", "hypothesis": "Everyone really likes the newest benefits ", "label": "ENTAILMENT"}
{"name":"mnli", "premise": "The new rights are nice enough", "hypothesis": "Everyone really likes the newest benefits ", "label": "ENTAILMENT"}
{"name":"hellaswag_hf", "query": "Roof shingle removal: A man is sitting on a roof. He", "choices": ["is using wrap to wrap a pair of skis.", "is ripping level tiles off.", "is holding a rubik's cube.", "starts pulling up roofing on a roof."], "gold": "3"}
Loading

0 comments on commit c82da79

Please sign in to comment.